Questions tagged [django-annotate]

297 questions
0
votes
1 answer

Django | Get count of articles per month

I got two models Article and Author implemented like this: class Author(models.Model): name = models.CharField(max_length=50) class Article(models.Model): name = models.CharField(max_length=50) author = models.ForeignKey(Author,…
0
votes
1 answer

Django is annotating wrong Count (possibly duplicates?)

I have a model ChatMessage that has a field sender which is a ForeignKey to User model. I'm trying to annotate a number of all the ChatMessage objects that haven't been read (eg. have seen_at__isnull=True). For a given user, there is only one sent…
Milano
  • 18,048
  • 37
  • 153
  • 353
0
votes
0 answers

Django QuerySet calculate sum of values according to a another attribute

I need some help for the creation of 2 queryset . Calculates the total of amounts of the objects considering the user as a common attribute. Given a specific user, calculate considering the categories, ordering the result in "in" and "out". This…
0
votes
1 answer

How to get count of more than one field from a table using Django queryset

here is my queryset queryset = (PrathamTeamMembers.objects. select_related('state', 'district', 'block', 'personnel_type'). filter(is_active=1). annotate(total_state=Count('state', distinct=True), …
Raju Singh
  • 455
  • 1
  • 6
  • 15
0
votes
1 answer

Django ORM: Get maximum value of a field with corresponding other fields values

I have this Table (Counters): cell_id tftralacc tfnscan thtralacc thnscan date_time 13997 10 360 94 360 2022-02-22 00:00:00+01 13997 0 360 0 360 2022-02-22 01:00:00+01 13997 0 360 0 360 2022-02-22…
ZSmain
  • 360
  • 1
  • 6
  • 20
0
votes
1 answer

How To Annotate Modified Related-Object Table In Django

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 =…
0
votes
1 answer

Getting top 3 most enrolled course by students

I am building a website where an instructor can create courses and students can enroll the courses. Is there any way to display top 3 most enrolled courses by students that an instructor has created? I have tried using…
Suraya Zulkifli
  • 105
  • 2
  • 8
0
votes
0 answers

Annotate dict data from related field Django

I have a model MyModel that contains a JSONField named calculo in MySQL and I want to annotate the value of premio to perform aggregated sum. How can I make this work? class MyModel(models.Model): ... calculo = JSONField(blank=True,…
Igor
  • 35
  • 9
0
votes
1 answer

Display items grouped in month together using django regroup

I have a model with following fields examname | month a1 Jan a2 Jan a3 Jan b1 Feb b2 March b3 March I want to display exams in each month grouped in months example Jan : a1…
0
votes
1 answer

order by a custom field on a django model

I am a junior developer from Greece and i need your help. In my django project, I have a model (Product) with a custom method(get_sale) that calculates a discount price after user inputs values . On my views ,there is a product list function that…
0
votes
1 answer

annotate an extra field but show just once in api response in django rest

I have to add an extra field using annotate in the get api. The field is something like count of a field of all the objects, not a particular object. And I want it to appear just once in the response of get api. If I use annotate, it appears on all…
Reactoo
  • 916
  • 2
  • 12
  • 40
0
votes
1 answer

Django Annotation Count with Subquery & OuterRef

I'm trying to create a high score statistic table/list for a quiz, where the table/list is supposed to be showing the percentage of (or total) correct guesses on a person which was to be guessed on. To elaborate further, these are the models which…
0
votes
0 answers

Annotating specific attributes of a Datetime Field - Cannot resolve keyword 'toordinal' into field. Join on 'created_at' not permitted

I'm developing a scoring system for posts on a website. It considers other relational fields (comments, views, and reactions_emojis) to have a more insightful way to order the results. But since it's not desirable to have popular posts, but too old,…
andrepz
  • 443
  • 6
  • 16
0
votes
0 answers

Django annotate count unique user from 2 tables

Is it possible to do an annotate count on technically 2 different tables, but same FK? Example: queryset =…
Spirconi
  • 164
  • 1
  • 12
0
votes
2 answers

Django ORM Annotate Count Items and Order by Specific Order?

I have a graph showing different licence types by region and how many active licences there are in each region. Is there a way to order the items in a specific order in the queryset to be output to the graph? These are my models: class…
kbdev
  • 1,225
  • 1
  • 13
  • 33