6,857 questions
3 votes
1 answer
51 views
Count distinct values of Subquery
I've got these models: class Component(models.Model) name = models.CharField(max_length=50) class ComponentAttribute(models.Model) component = models.ForeignKey(Component) context = ...
2 votes
1 answer
58 views
Django filter an m2m relation by a list of inputs (which must all match)
Let's take some Store and Book models as examples: class Book(Model): title = CharField(...) ... class Store(Model): books = ManyToManyField('Book', blank=True, related_name='stores') ...
0 votes
1 answer
77 views
Django REST Framework `pagination_class` on ViewSet is ignored
Describe the Problem I have a ModelViewSet in Django REST Framework designed to return a list of Order objects. To improve performance, I'm trying to implement custom pagination that limits the ...
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 ...
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
53 views
QuerySet returns empty in django
I am trying to get the book list with 3 conditions: Accession number (character varying,10) Physical location (character varying,20) Book status Based on the user input: Accession number should be ...
0 votes
1 answer
66 views
How to check if a django queryset has a matching manytomany + foreignkey?
I try to check - in Django - if a user-made queryset has two fields which match 100%. class Foo(models.Model): # ... free_field = models.ForeignKey(FreeModel, ...) must_match_m2m_field = ...
0 votes
1 answer
88 views
How to deal with recurrence dates in Django?
I'm currently developing a Gym website and using django-recurrence to handle recurring training sessions. However, I'm unsure how to work with recurrence dates in a Django QuerySet. Specifically, I ...
1 vote
1 answer
88 views
Django queryset annotate sum of related objects of related objects
I have class Book(models.Model): title = models.CharField(max_length=32) class Table(models.Model): book = models.ForeignKey(Book, related_name='tables') class TableEntry(models.Model): ...
0 votes
0 answers
55 views
Create a Django custom Transform from an annotation
I've got an annotation for a foreign key field that I use frequently and would like to write a custom Django Transform which behaves similar to the annotation. The annotation that does what I would ...
0 votes
1 answer
52 views
Custom generic istruthy/isfalsy lookup doesn't work on Foreign Keys
In my Django project, I'm in need of a generic istruthy/isfalsy lookup that would work on any type of database field. Example of models.py: class MyModel(models.Model): my_charfield = models....
-1 votes
1 answer
73 views
How to add element in Django QuerySet?
I have model Report and GroupReport. They have many-to-many relation. I can easily get QuerySet[GroupReport] for all GroupReport instance: Group Name 1: Report Name 1 Report Name 2 Group ...
1 vote
0 answers
52 views
Count distinct on an aggregated dimension
I have the following model: class Historique(models.Model): type_of_object_choices = ( (1, 'Cadeau') , (2, 'Offre') , (3, 'Commentaire') ) event_type_choices = ...
1 vote
0 answers
52 views
Django QuerySet performance
I'm debugging sitemap generation, and finally have the following snippet: items=Event.objects.all() for limit in (1000, 10000, 20000): events=items[:limit] ...
4 votes
1 answer
283 views
Django admin inline performing too many queries when having additional ForeignKey
I have the following Django models: from django.db import models from django.utils.translation import gettext_lazy as _ class Enterprise(models.Model): nome = models.CharField(max_length=255) ...