Questions tagged [django-annotate]

297 questions
2
votes
3 answers

How to get extra columns in group by in Django?

I just want to include ID(and other fields) as well in the result of an annotate with values (i.e group by). I'll explain with an example (below is just an example, please suspend your disbelief on this bad model), class Book(models.Model): name…
2
votes
2 answers

Django - Query : Annotate Queryset with Related Model fields

I have following Schema in Django with PostgreSQL. Class Person (models.Model): name = models.CharField(max_length=255) email= models.CharField(max_legth = 255) created_at = models.DateTimeField() Class PersonTask(models.Model): …
JithPS
  • 1,167
  • 1
  • 7
  • 19
2
votes
2 answers

How to create a weekly summary of dated records in model django

I have a model that has entries for each day. I want to create a queryset with a subtotals (annotates) for a selection of fields in the model. This is what i have so far. Here is the model (or some of it!) class Shiftlog(models.Model): date =…
Paul West
  • 81
  • 8
2
votes
1 answer

Django annotate subquery's aggregation

So I have three models class Advert(BaseModel): company = models.ForeignKey(Company, on_delete=CASCADE, related_name="adverts") class Company(BaseModel): name = models.CharField(max_length=50) class OrderRating(BaseModel): …
2
votes
5 answers

Django Queryset with annotate

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…
KonriMarzec
  • 45
  • 1
  • 6
2
votes
5 answers

Django querysets optimization - preventing selection of annotated fields

Let's say I have following models: class Invoice(models.Model): ... class Note(models.Model): invoice = models.ForeignKey(Invoice, related_name='notes', on_delete=models.CASCADE) text = models.TextField() and I want to select Invoices…
radoh
  • 4,554
  • 5
  • 30
  • 45
2
votes
1 answer

How to aggregate with prefetch related and prefetch

I Have two models clinic and ClinicCredits: I want a list of clinics withsum of available balance The Problem is if i use annotate i have to loop for avalibale alance with queryset of cliniccredits : class Clinic(models.Model): """ clinic…
Ashish
  • 459
  • 2
  • 10
2
votes
1 answer

How to convert with annotate String to Bool with Django ORM

My question is as follows. There is a request Thread.objects.all().annotate(is_marked=Count('mark', Q(mark__username='gou'))) in SQL it looks like this SELECT "api_thread"."id", "api_thread"."conference_id", "api_thread"."title", …
2
votes
0 answers

Annotation field is str instead of datetime.datetime

Example I'm trying to annotate objects using data from a related model's DateTimeField. class Author(models.Model): name = models.CharField(max_length=256) class Book(models.Model): name =…
2
votes
1 answer

How to create an annotation in Django that references two related models

I'm trying to add an annotation to a QuerySet that is True/False when the value of a field on one related object is less than the value of a field on a different related object. Here are some models for an example: class…
Bryan Hurst
  • 85
  • 11
2
votes
1 answer

Django annotate, combine multiple related values onto same instance

I have a django app with the following models: class Person(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) class Job(models.Model): title =…
rurp
  • 1,376
  • 2
  • 14
  • 22
2
votes
0 answers

Annotation from regex in When() conditional expression

I need too annotate the result of a regex lookup in a When() conditional expression using the then parameter, DB is Postgresql. For example you can use the SubStr() function to get a substring of a field, I want to be able to use a regular…
2
votes
1 answer

Grouping queryset by date

I have a class which contains a date as well as some other fields. What I'm trying to figure out is a query which will return me each of the items, grouped by the date. So, given the following class:- class Item(models.Model): item_date =…
bodger
  • 1,112
  • 6
  • 24
2
votes
1 answer

DRF annotated queryset filtering with django-filter

can anyone help with filtering in DRF. I have some products models, say Product and manager ProductManager: class ProductItem(Model): price = DecimalField() class Product(Model): items = ManyToManyField(ProductItem) priceman =…
2
votes
1 answer

Django annotate model and filter for specific value

I'm trying to get into djangos annotate, but can't quite figure out how it works exactly. I've got a function where I'd like to annotate a queryset of customers, filter them and return the number of customers def my_func(self): …
NakedPython
  • 920
  • 2
  • 7
  • 27