I am trying to sum Django query with a condition. Suppose I have got some data like this: And these types are string and they have values, like x = 1, y = 25, z = -3 How can I sum up all the values without a loop? Currently using a loop. Answer To perform the calculation inside the database
Tag: django-queryset
Access Django M2M values in queryset without looping
I receive some data through Ajax that allows me to do some filtering on a model that has some m2m relations (say Model). I get a queryset, say “content” that I then need to send back using json. Here are simplified lines of code: It was working fine but the project changed and I now need to include the values
django FieldError: Cannot resolve keyword ‘category_name_contains’ into field. Choices are: category, description, id, image, name, order, price
I want to build a food delivering app, but facing this error. “FieldError: Cannot resolve keyword ‘category_name_contains’ into field. Choices are: catego ry, description, id, image, name, order, price”. This is the query.py: This is the views.py This is my models.py: Here’s the traceback: So this my problem I have tried many solutions still not getting the problem here. As
How to apply a for statement to simplify repeated code in Django queryset
The filter is applied according to team_parameter(request.GET.get(‘team)) and it has very repetitive code. At the end of the if statement, no filter is applied only if team_parameter is ‘ALL’. I think a for statement is necessary to minimize this code, but I did not know how to apply it, so I asked a question. Please let me know which loops
Flitering django queryset based on ManyToManyField
In my Request model, there is a field requested_to which is a ManyToManyField I want to filter queryset of Request model where a organization_user is not in requested_to Answer You can filter with: Django makes LEFT OUTER JOINs when you filter on a ManyToManyField (or a reverse ForeignKey), so here we exclude all Requests where organization_user is a member of
Django merge QuerySet while keeping the order
i’m trying to join together 2 QuerySets. Right now, I’m using the | operator, but doing it this way won’t function as an “append”. My current code is: I need the elements from querysetA to be before querysetB. Is it even possible to accomplish while keeping them just queries? Answer This can be solved by using annotate to add a
About converting custom functions in models into annotations in custom managers/querysets
Being new to Django, I’m starting to care a bit about performance of my web application. I’m trying to transform many of my custom functions / properties which were originally in my models to …
Getting the current date (and time) in Django
I was wondering what is the best way to get the current date in a django application. Currently I query the python datetime module – but since I need the date all over the place I thought maybe Django already has a buildin function for that. e.g. I want to filter objects created in the current year so I would
Django-queryset join without foreignkey
model.py class Tdzien(models.Model): dziens = models.SmallIntegerField(primary_key=True, db_column=’DZIENS’) dzienrok = models.SmallIntegerField(unique=True, db_column=’ROK’) class Tnogahist(…
sql “LIKE” equivalent in django query
What is the equivalent of this SQL statement in django? SELECT * FROM table_name WHERE string LIKE pattern; How do I implement this in django? I tried result = table.objects.filter( pattern in …