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
DRF Aggregating related collections

So lets say i have a Box model. The box model works like a "master" model, and have lots of foreign keys, thus having relations to lots of other models.

Now for example i have a related model Cost, and the Box models has a column costs that is a one-to-many relationship to the Cost model.

When i make a basic REST endpoint to get all my Boxes, i get an array of each cost included. I have used to_representation to only get the cost value for each record in the cost model.

This gets kind of tedious however, as i have to reduce the costs to a single number on the client side (might be slow when my dataset grows)

So whats the best way to aggregate the costs on the server so i can return a single number for all the related costs for the Box?

Should this be done in the serializer, or is there a better place to put this logic? Im using DRF's viewsets.ViewSet as a base in my views.

Should i aggregate the total costs in python, or do a SUM on the database level?

[EDIT]

At the moment im just augmenting my serializer data


for value in serializer.data:
# costs is an list of decimals
value['costs'] = sum(value['costs'])


I was wondering if there was a better way.

Thanks!

/r/django
https://redd.it/8bw9gt
How can I use Django REST Framework and django-filter to filter records based on month and day (ignore year)?

There are some scenarios where I would like to be able to filter records based on month and possibly day, but ignore the year. For example, listing the users who registered in a particular month (regardless of year), or listing the orders that were placed in the first half of a particular month (regardless of year).

Here is the relevant documentation:

https://www.django-rest-framework.org/api-guide/filtering/

https://django-filter.readthedocs.io/en/latest/ref/filters.html

I didn't see anything at either of those links that addresses this, though maybe I overlooked it. Any help would be appreciated. Thanks!

/r/django
https://redd.it/8brodi
How to set multiple access keys(Secret_key and Id) for a user for sandbox and live

How to set multiple access keys(Secret_key and Id) for a user for sandbox and live API access while also have different keys for login.
GitHub examples will be perfect. Thanks

/r/django
https://redd.it/8bsq4m
How to limit number of lines per code?

Hello all,

I currently spit out images from a DB into a new row. I want to limit the number of columns by 3 and create a new row.

How can I achieve this? Here is my HTML:

<table>
{% for item in img %}
<tr>
<td>
<img stuff><checkbox stuff>
</td>
</tr>
{% endfor %}
</table>

Thanks!


/r/flask
https://redd.it/8c54l0
When is it necessary to import a submodule when the parent module is already imported?

I have been scouring the docs for a while and I cannot find any reference to this.

Sometimes you are able to use a submodule without importing it, by importing the parent module. Like so:

$ python3
>>> import os
>>> os.path.join('/home', 'root')
'/home/root

Other times it is not possible to do this, like so (on Python 3.5.2):

>>> import email
>>> m = email.message.Message()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'email' has no attribute 'message'
>>> import email.message
>>> email.message.Message()
<email.message.Message object at 0x7f5932853f60>

I asked on #Python because I thought this was a bug in the submodule, when I received a surprising response:

> This is not a bug. Your code is wrong. You cannot use the submodule without importing it. Sometimes it works, but you cannot count on it.

Is this true? If so, I didn't know until now that the import system worked this way! Somewhat more confusing is that the first example is not rare to come across in the official documentation or other documentation sources, from my experience.

/r/Python
https://redd.it/8cccdg
Running scheduled tasks with flask and sending users an email with the results

I just want to know if this kind of thing is possible. I want to make a flask app where users login and submit some information about products they like. Once an hour or so python would take their submitted information and use that in an API call to some service, do analysis, and then email the user the results. I know how to do each of these things alone outside of flask, I am just wondering if it is possible to deploy a web app to do this using flask before I go though and start investing time on research/making it. Thank you.

/r/flask
https://redd.it/8cbexf
Django docker, uwsgi, production configuration

So i'm curious if anyone has any input about best practices for running django in a container. I have a djano application i'm running in a container in a kubernetes cluster and I want to make sure i'm following best practices. So i'm curious specifically about

1. Docker settings for django ( securing things, not running uwsgi as root, etc)
2. uwsgi settings:
Right now uwsgi is talking to the outside world through a azure load balancer but I will be adding an nginx ingress soon so we can have SSL termination for producation and staging. Also, I am having trouble understanding what combination of uwsgi settings I need to apply for speed and robustness and allocating enough resources in the kubernetes spec to best satisfy uwsgi. The uwsgi documentation is somewhat confusing and my understanding of how python is managed by wsgi is somewhat lacking specifically around threads, processes, and workers. Also when to use thunder-lock and not running in multiple interpreter mode.


[Dockerfile](https://gist.github.com/dfcarpenter/15246c80ec6d76516b3a3922028c0a5f)
[uwsgi](https://gist.github.com/dfcarpenter/5ff2d200da36188c73605f0842fd9340)



/r/django
https://redd.it/8capty
Found this on r/vexillology thought it looked cool

/r/Python
https://redd.it/8ccn6b
How should I approach Django to make the learning process less overwhelming?

This is another attempt of mine to learn Django. Since there seems to be nothing better than the official Polls tutorial, I have decided to stick with it. No doubt, it's written in a succinct and user-friendly manner. And yet, Django seems so powerful and huge that I get frustrated. Is it okay that things don't click in my head yet?

Do I need to make sure that I understand everything in every particular part of the tutorial to proceed with the next?

/r/djangolearning
https://redd.it/8cdqqe
Deploying a Django Application on Ubuntu VPS using Gunicorn, MySQL and NGINX (tutorial)
https://www.shellvoide.com/hacks/installing-django-application-with-nginx-mysql-and-gunicorn-on-ubuntu-vps/

/r/django
https://redd.it/8cey0z
Does the data persist in a redis database?

I am going to be using rabbitmq as a message broker and Redis as results backend for a flask app. I signed up for redis labs and on the free version, you get 30 MB of storage.

The use case will be pulling data from some api's on data.gov and then loading the data into a website asynchronously. The data that is pulled isn't that large, maybe ~few hundred kB. However, over time I could max out my limit. Or does redis(labs) only keep that data for a certain period of time and then it get's deleted?

/r/flask
https://redd.it/8cab9h
TIL that starting from 3.7 dicts are officially ordered

Since 3.6 [dicts preserve order because of the new “compact” representation](https://docs.python.org/3.6/whatsnew/3.6.html#whatsnew36-compactdict) but it was just an implementation detail and we were told not to rely on it.

But in 3.7 dicts **officially** became ordered:

> Performing `list(d)` on a dictionary returns a list of all the keys used in the dictionary, **in insertion order**

(from https://docs.python.org/3.7/tutorial/datastructures.html#dictionaries)

TBH, I'm not sure that this is a good idea. I can see a number of problems with this change in future:

1. Now people will start relying on the order of elements in a dict and if Python developers want to change the implementation in future they will **have to** preserve the order as well.
2. It forces developers of other Python interpreters to implement this feature as well even if it will cost them some additional work or decrease performance.
3. It makes `collections.OrderedDict` redundant which could lead to confusion.

What do you think about this decision? Why do you think they made it?

/r/Python
https://redd.it/8cf4rf
Database design (lots of columns / fields)

I currently have a model that has over 100 fields on it. I'm not sure the best way on how to optimise this. The options i've thought of so far:

1. Keep as is and use `Model.objects.only('field1', 'field2')` with each query i make
2. Have a main model then have a 2nd "Data" model that one to ones with the main model then query the extra data that way.
3. Convert a lot of the columns to be able to be stored in a single PSQL JSON column, i'm not sure if this causes filter / index issues down the lines as a decent amount of the columns are indexable.

Is there another option that is better than any of these 3 or which of the 3 would give me the most flexibility going forward?

Edit**

My data is coming from an external source which looks like: https://gist.github.com/dan-gamble/fac2becbd56dc6fe88f281a64823a2f4

Some of the keys i don't need so they got dropped, the Nation / League / Club all are FK's to another object. The rest is currently stored on a `Player` model. The columns that have a value assigned to them are stats and can be filtered on.

/r/django
https://redd.it/8cfr2q