4,076 questions
0 votes
0 answers
69 views
Django ORM: Add integer days to a DateField to annotate next_service and filter it (PostgreSQL)
I am trying to annotate a queryset with next_service = last_service + verification_periodicity_in_days and then filter by that date. I am on Django 5.2.6 with PostgreSQL. last_service is a DateField. ...
1 vote
1 answer
46 views
How to use Django Q objects with ~Q() inside annotate(filter=...) to exclude a value?
I'm refactoring a legacy Django Job to use annotate with filtered Count aggregations instead of querying each record individually (avoiding the N+1 problem). I want to count the number of related ...
3 votes
1 answer
78 views
Django/PostgreSQL: Unique constraint with a dynamic condition like expires_at > now()
I'm building a secure OTP system in Django. The model looks like this: class OTP(models.Model): MAX_ATTEMPTS = 3 DEFAULT_EXPIRE_IN_MINUTES = 1 class Purposes(models.IntegerChoices): ...
0 votes
1 answer
42 views
Django Multi-Database ValueError: "Cannot assign Role object: the current database router prevents this relation"
I'm encountering a ValueError in my Django application when trying to save a User object with a related UserRoleAssociation in the admin interface. The error occurs in a multi-database setup where ...
2 votes
1 answer
95 views
How do I write a Django ORM query that returns a match based on an intersection of lists?
If I have, for example: from django.contrib.postgres.fields import JSONField class MyModel(models.Model): data = JSONField() GIVEN a model instance obj1, where data == ['apple', 'banana', '...
1 vote
1 answer
60 views
Django annotate with ExtractMonth and ExtractYear doesnt extract year
I have this model: class KeyAccessLog(models.Model): key = models.ForeignKey( Key, related_name="access_logs", on_delete=models.CASCADE ) path = models.CharField(...
-3 votes
1 answer
83 views
How to get a moving average (or max) in time period using Django ORM
I am trying to work out if it's possible/practical to use the Django ORM to get the highest value in an arbitrary timebox out of the database. Imagine a restaurant orders ingredients every day, we ...
0 votes
0 answers
44 views
Django filter objects which JSONField value contains in another model's JSONField values LIST of same "key"
I'm not sure if the title of this post made you understand the problem but let me explain: I have models: class ItemCategory(models.Model): name = CharField() class Item(models.Model): brand =...
0 votes
1 answer
35 views
Django ORM, append external field to QuerySet [duplicate]
I have a simple queryset resulting from this: Books.objects.all() Books is related to Authors like this: Class Books: ... author = models.ForeignKey(Authors, on_delete=models.CASCADE) Class ...
-1 votes
1 answer
41 views
run one time for Django model calculated property
class Company_Car(models.Model): @property def days_left(self): print("RUNNED PROPERTY") if self.date_valid is not None and self.date_valid >= datetime....
1 vote
3 answers
120 views
Django `bulk_update()` : Update Different Fields for Each Record in a Single Query
I have two model instances, rec1 and rec2, but each has different fields updated: rec1 = MyModel(id=1, name="John") # Only 'name' is changed rec2 = MyModel(id=2, age=30) # Only 'age'...
0 votes
1 answer
71 views
Unable to display data from QuerySet in Django templates
I'm trying to implement a simple search for data on a website. If we specify the a tag in the "search_results.html" template, then all the information that is in it is not displayed, but if ...
3 votes
1 answer
99 views
backward lookup is not working in django 5.x
We are migrating our django app from django==3.2.25 to django==5.1.6. OneToOneField, ManyToManyField are giving errors on revers lookup. Create fresh setup. python -m venv app_corp_1.0.X ./app_corp_1....
0 votes
0 answers
103 views
Django ORM sporadically dies when ran in fastAPI endpoints using gunicorn [duplicate]
Other related Questions mention using either django.db's close_old_connections method or looping through the django connections and calling close_if_unusable_or_obsolete on each. I've tried doing this ...
0 votes
1 answer
137 views
Writing Django Func() Expression with multiple parameters and specify the order
I'm using Func() Expressions to use this answer and compute the difference between two dates in business days: class BusinessDaysBetween(Func): """Implementation of a Postgres ...