Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
How do you concatenate the values of a many-to-many field into a single field in Django?

I have a model that I am querying against, let's call it M. M has a many-to-many relationship with another model, let's call that R. For a queryset of Ms, I want to add an annotation that contains the names of the related Rs as a string of comma separated values. Is there a way to do this in Django?

/r/django
https://redd.it/7ertd9
Do you recommend using Digital Ocean 1-Click Django on Ubuntu Image ?

I am getting into deploying a site that uses postgres on a server. I thought Digital Ocean 1-Click Django on Ubuntu Image might be better than doing everything myself form scratch since I don't have the experience.

Most of the tutorials I find start from scratch. I have found that having some things already configured like a postgres user and database, makes me worry that things will break if I for example create a database specific for my project.

Do you think this image will help ease the whole process or it will restrict me ?

/r/django
https://redd.it/7eutn6
Live chat on website that has backend of Django

Hey guys.

I have recently started learning django and I am making a website. I wanted to put live chat in the website but I noticed that Django doesn't have that. Is there any way that I can make a live chat on my website with Django or any easy way to do this?

I know that socket.io for node.js is good for live chats, but since I am new to web development I rather stick to one if possible. Also I am not sure how I would deploy a website that has 2 backend programming languages.

Thank you!

/r/django
https://redd.it/7ev08o
[AF] File to be downloaded doesn't update / firefox caching problem

Sorry for the vague title. The situation is as follows: I have a download link on a page, and after clicking on the download link, the user is sent a .csv file. The user then edits some information on the site, which should appear on the .csv file. After editing, the user returns to the downloads page and downloads the same file again. However, the updates are not visible and the old file is being downloaded.

After deleting the firefox cache or opening site in private window, the new file is presented. The problem does not exist in chrome.

Is there some way to disable this behavior?



/r/flask
https://redd.it/7erw9a
Adding Jekyll to create documentation page onto Flask web application?

Hello, I was wondering if anyone had experience using Jekyll in order to create a nice documentation page, and adding it onto a Flask web application.

I've already create the bulk of the web application with Flask. I was hoping to create an @app.route('/documentation', methods=['GET]) which would direct users to a documentation page, which would be made with Jekyll. I'm new to this and a bit confused about where to start. Any resources or help would be greatly appreciated. Thank you!

/r/flask
https://redd.it/7ewpla
Can someone explain this black magic?

/r/Python
https://redd.it/7ep6ci
Projects for a beginner?

Which projects should a beginner at Python start at, and then progress too? I don't have anything in mind right now, so I don't have a lot of ideas.

/r/Python
https://redd.it/7exjil
Help serving static file sitemap.xml

Hello,

I am having issues serving my sitemap.xml file. It works on localhost:5000/sitemap.xml but not on the live domain. I am hosting this on a Linux server with Apache2

This is what I get on the live domain:


"Not Found


The requested URL was not found on the server. If you entered the URL manually please check your spelling and
try again."



This is my code:

import _mysql
from flask import Flask, request, jsonify, redirect,
url_for,render_template, send_from_directory,session,
abort,json,flash
import os
import prettyprint
import sys

reload(sys)
sys.setdefaultencoding('utf8')

PATH = os.path.dirname(os.path.abspath(__file__))
app=Flask("Locals",template_folder=os.path.join(PATH,
'templates'),static_folder='static')

@app.route('/sitemap.xml')
def static_from_root():
return send_from_directory(app.static_folder, request.path[1:])

@app.route("/", defaults={'state': None, 'city': None, 'keyword':
None})
@app.route("/<state>", defaults={'city': None, 'keyword': None})
@app.route("/<state>/<city>", defaults={'keyword': None})
@app.route("/<state>/<city>/<keyword>")
def index(state,city,keyword):

& at the bottom:

if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=5000)

/r/flask
https://redd.it/7erilu
How to join two tables across DBs using Django ORM?

I want to perform the equivalent SQL query using django's ORM:

USE TRADING_DB;
SELECT NAME,
BUY_UNITS, BUY_NAV,
SELL_UNITS, SELL_NAV,PANDL
FROM
MUTUAL_FUND_POSITIONS
INNER JOIN MUTUAL_FUND_DB.MF_NAV_DATA AS NAV ON NAV.ISIN =
MUTUAL_FUND_POSITIONS.MF_ISIN
WHERE STATUS='OPEN' AND CLIENT_ID="Z712A";

Let's assume I have the following models:

class MutualFundPositions(models.Model):
pos_id = models.AutoField(db_column='POS_ID', primary_key=True)
client_id = models.CharField(db_column='CLIENT_ID', max_length=45)
mf_isin = models.CharField(db_column='MF_ISIN', max_length=100)
buy_units = models.DecimalField(db_column='BUY_UNITS',
max_digits=10, decimal_places=5)
buy_nav = models.DecimalField(db_column='BUY_NAV', max_digits=10,
decimal_places=5)
sell_units = models.DecimalField(db_column='SELL_UNITS',
max_digits=10, decimal_places=5)
sell_nav = models.DecimalField(db_column='SELL_NAV', max_digits=10,
decimal_places=5)
status = models.CharField(db_column='STATUS', max_length=45)
product = models.CharField(db_column='PRODUCT', max_length=45,
blank=True, null=True)
pandl = models.DecimalField(db_column='PANDL', max_digits=10,
decimal_places=2, blank=True, null=True)

class Meta:
managed = False
db_table = 'MUTUAL_FUND_POSITIONS'

class MfNavData(models.Model):
scheme_code = models.IntegerField(db_column='SCHEME_CODE')
isin = models.CharField(db_column='ISIN', primary_key=True,
max_length=45)
name = models.CharField(db_column='NAME', max_length=500)

class Meta:
managed = False
db_table = 'MF_NAV_DATA'
unique_together = (('isin', 'name'),)

How would I do that with Django's ORM? The idea is that I want a single query, using Django's ORM.


MutualFundPositions table is on TRADING_DB and MFNAVDATA is on MUTUAL_FUND_DB.


/r/django
https://redd.it/7eyvlm
How do I make something actively run in the background?

Hi everyone. Currently I'm doing a project that is essentialy a web-browser game like the ones that used to be popular 10 years ago. It's going to be RPG-like thingy.
Each player has their own character with individual statistics. Those characters are stored as an extension of `User` model with use of `OneToOneField`. They have variety of stats like HP, max HP, EXP, EXP till next level, level, etc.
Everything so far is fine, but let's say a character is below max hp and his health regenerates over time. How do I do that? I mean, how do I make that something actively updates in the database? Same would go for experience and levelling up. How do I do that? Do I make some kind of Python script that would take care of it? Or is there some Django feature that helps handle those cases?
The only place where I can think of that handles different actions is views, but I can't imagine how I would put something there to run in the background since each view has to be rendered by request.

/r/djangolearning
https://redd.it/7eubgk
Introducing CalPack - a python module to make it easy creating and parsing custom packets
https://github.com/KronoSKoderS/CalPack

/r/Python
https://redd.it/7eyyyc
Celery subqueues? (implementing sequential message delivery)

Hey there!

I'm trying to implement a sort of chatbot system, and I need to have my celery tasks processed sequentially per user. That means each user needs to have their messages sent as FIFO, but the users need to be processed randomly or in round-robin.

I've been reading about task chains, groups and trees, but all of these celery features seem to require providing all tasks at once, whereas I need to add tasks dynamically.

My reasoning was to have a dedicated queue per user, and enable concurrency on the worker. That way I could assure the delivery order and avoid one user blocking the rest of the chats.

Is there a way I can route tasks in Celery so I get the desired behavior? Ideally, I'd set up a worker to process the `messages` queue, and then route tasks to something like `messages.contact.<contact_id>` as they arrive.

thanks, and happy Thanksgiving to all of you in the US!

/r/Python
https://redd.it/7ezap7
Django Rest Serializer or Model property.

When i need a custom field at REST API. I can use `serializers.SerializerMethodField` but Model property does the same.
Can you tell me which better or when we using each of them?

/r/django
https://redd.it/7exzfg
Download all episodes from https://talkpython.fm/episodes/all

Hey guys, i wrote this little script to easily download all the episodes of this amazing podcast
I hope it can be useful to some of you, obviously feel free to give me feedback on it

https://github.com/cece95/TalkPythonDownload/tree/master

/r/Python
https://redd.it/7ezttz
State of Version Control System of Python modules: 32.95% no link to VCS found
https://szabgab.com/state-of-version-control-in-python-modules.html

/r/Python
https://redd.it/7f02nh
Anyone can do a layman's guide to accessing a jupyter remotely from another computer?

do explain in simple terms!

/r/IPython
https://redd.it/7ezpe8