Questions tagged [django-annotate]
297 questions
0
votes
2 answers
How to find the percentage of employee scores using django
I have a table called Task. It captures task, task score and the employee it is assigned to.
I want to calculate the performance of the employee using his total task scores for the week (duration should be dynamic. preferably input field) and the…

Thaddeaus Iorbee
- 63
- 9
0
votes
1 answer
Error when changing from Sqlite3 into Postgresql in Django, with annotation and pagination
I'm learning Django and used Sqlite3 for my site until today, I changed the database into Postgres.
My site kind of mimics Facebook. I have a ListView that will list all posts. In get_queryset(), I annotate each post with some additional info, such…

Khai Hoan Pham
- 86
- 1
- 2
- 7
0
votes
2 answers
Making of a customer report based on sales
I am trying to make a customer wise sales report, in it there will customers listed with their total number of sales, total amount, total paid and balance of sales occurred in the selected time period.
models:
class Customer(models.Model):
name…

Pulath Yaseen
- 395
- 1
- 3
- 14
0
votes
1 answer
How to find objects with the most similar fields?
This is the model I have defined:
class Book(models.Model):
authors = models.ManyToManyField(to='Author')
category = models.ForeignKey(to='Category')
tags = models.ManyToManyField(to='tags')
# some other fields
How can I find ten…

Mehran Isapour
- 58
- 6
0
votes
1 answer
Django PDF Render, aggregate template tag shows up with code included
In views.py, I have the following function to render a PDF view:
def agent_render_pdf(request, *args, **kwargs):
pk = kwargs.get('pk')
agent = get_object_or_404(Agents, pk=pk)
invoices = Invoice.objects.filter(agent=agent)
invoice_total =…

FaxMeBeer
- 1
- 1
0
votes
1 answer
Django annotate by adding months to date field
from datetime import timedelta
from django.db.models import DateTimeField, ExpressionWrapper, F
MyModel.objects.annotate(
date_plus345=ExpressionWrapper(F('creation_date') + timedelta(days=345),
output_field=DateTimeField()
…

Hyz
- 1
0
votes
1 answer
Django admin sorting: sorts by integer annotation like it's string
I need django(2.1.1) admin to sort changelist page of a model by a calculated value. This is what I have in the admin class:
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.annotate(
…

javad
- 29
- 2
0
votes
1 answer
Django convert Date of Birth model field to Age
I have a Django class to convert the date of birth (dob) field in my model to age and annotate the result to a queryset.
class CalculateAge(Case):
def __init__(self, model, field, condition=None, then=None, **lookups):
today =…

24thDan
- 113
- 1
- 9
0
votes
0 answers
How to annotate a Django QuerySet with a custom Function?
I need to annotate a queryset using a custom function agr_suitable_code on a model field.
which looks like this
def strip_accents(s):
return ''.join(c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) !=…

David Louda
- 498
- 5
- 19
0
votes
1 answer
Django annotate by foreign key
This is my models.py:
class Category(models.Model):
category_des = models.CharField(max_length=250)
class Type(models.Model):
type_des = models.CharField(max_length=250)
class Product(models.Model):
category_id =…

amine4392
- 79
- 6
0
votes
1 answer
How to use annotate to Count items from a Django QuerySet
I am modeling Applications and the ways they are installed on user's devices as follows:
class WindowsUserApplications(models.Model):
application_name = models.CharField(max_length=60, null=False, unique=True)
category =…

Bernardo
- 5
- 6
0
votes
1 answer
Annotating with the count of a subquery filtering by jsonb fields in Django
I have a single model with a jsonb field. There is a value inside this jsonb field that can be shared amongst other rows. I am trying to get the count of a subquery while filtering by this jsonb field.
Some pseudo code of what I have been attempting…

anthony-dandrea
- 2,583
- 7
- 26
- 46
0
votes
1 answer
Username is not showing but user id is showing in html template in Django
Below is my Model :
class Problem_Solved(models.Model):
user_ref = models.ForeignKey(User, on_delete=models.CASCADE)
contest_ref = models.ForeignKey(Contest, on_delete=models.CASCADE)
problem_ref = models.ForeignKey(Problem,…

Nishit Popat
- 54
- 10
0
votes
1 answer
Annotate JSONField on-the-fly and retrieve keys via values
I annotate a JSONField on-the-fly like this. Since this is not a field of a model I think the model is not relevant. This example should be valid for any queryset.
>>> from django.db.models import F, Func, JSONField, Value
>>> queryset =…

Stefan_EOX
- 1,279
- 1
- 16
- 35
0
votes
1 answer
Django annotate existance in related field
My models:
class Character(models.Model):
name = models.CharField(max_length=100)
class Item(models.Model):
name = models.CharField(max_length=100)
class Equipment(models.Model):
owner = models.ForeignKey(Character, on_delete =…

Tomasz Brzezina
- 1,452
- 5
- 21
- 44