django-annotate

Django annotate field value from external dictionary

Django annotate field value from external dictionary Question: Lets say I have a following dict: schools_dict = { ‘1’: {‘points’: 10}, ‘2’: {‘points’: 14}, ‘3’: {‘points’: 5}, } And how can I put these values into my queryset using annotate? I would like to do smth like this, but its not working schools = SchoolsExam.objects.all() …

Total answers: 1

How to group by and get latest record in group contain all of fields in Django?

How to group by and get latest record in group contain all of fields in Django? Question: Imagine we have a table like this: id name type created_at 1 James male 2022-03-02 2 Jane female 2022-04-02 3 Kirk male 2022-03-04 4 Sarah female 2022-04-04 5 Jason male 2022-03-05 And i want to group by type …

Total answers: 2

Is it possible to use aggregate on a queryset annotation?

Is it possible to use aggregate on a queryset annotation? Question: I am using annotate on a django queryset: class MyModel(models.Model): my_m2m = models.ManytoManyField() my_qs = MyModel.objects.all().annotate(total_m2m=Count(‘my_m2m’)) This yields the desired result. E.g: >>> my_qs[0].total_m2m >>> 3 How do I use aggregate to count the total number of my_m2m in the queryset? E.g. >>> my_qs.aggregate_m2m …

Total answers: 1

Django: Perform GROUP BY over a view queryset

Django: Perform GROUP BY over a view queryset Question: I need to perform group by over a queryset of objects already filtered by standard django filters in order to filter by how many objects in queryset are related to same foreign key object. My code now (does not work): class CustomerTicketsViewSet(AbstractViewSet): queryset = CustomerTicket.objects.all() serializer_class …

Total answers: 1

Rendering highest integer value from three models instances in django

Rendering highest integer value from three models instances in django Question: I have three models which are related all to one model. class MyModelParent(models.Model): name = models.CharField(max_lenght=36, blank=True) def __str__(self): return self.name or "" class MyFirstModel(models.Model): mymodelparent = models.ForeignKey(MyModelParent, related_name="first_models", blank=True, Null=True, on_delete=models.CASCADE ranking = models.IntegerField(max_lenght=36, blank=True) def __str__(self): return self.name or "" class MySecondModel(models.Model): …

Total answers: 1

How To Annotate Modified Related-Object Table In Django

How To Annotate Modified Related-Object Table In Django Question: I have a small business app which can be simplified as following models: class Client(..): name = CharField(…) class Sale(..): client = ForeignKey(Client, …) item = CharField(…) time = DateTimeField(…) value = DecimalField(…) class Receive(..): client = ForeignKey(Client, …) time = DateTimeField(…) value = DecimalField(…) Now …

Total answers: 1

Django Queryset with annotate

Django Queryset with annotate Question: I am writing one method in Django Manager model. I want to write method that finds out number of all sold copies (books) per author. I have two models and method written in Manager. My problem is that method should also be chainable from any Author queryset, for example something …

Total answers: 5

Using Subquery to annotate a Count

Using Subquery to annotate a Count Question: Please help me I’ve been stuck on this for way too long 🙁 What I want to do: I have these two models: class Specialization(models.Model): name = models.CharField(“name”, max_length=64) class Doctor(models.Model): name = models.CharField(“name”, max_length=128) # … specialization = models.ForeignKey(Specialization) I would like to annotate all specializations in …

Total answers: 2

How to subtract two annotated columns on Django QuerySets?

How to subtract two annotated columns on Django QuerySets? Question: I need to be able to sort on the aggregate of two annotated columns So I’d like to do something like this: c = c.annotate(metric=Sum(‘results__metric’)) c = c.annotate(metric_prior=Sum(‘results__metric_prior’)) c = c.annotate(variance=F(‘metric’)-F(‘metric_prior’)) #doesn’t work, for demonstrative purposes only and then: c = c.order_by(‘variance’) Does anyone know …

Total answers: 2