Django: Group by and return multiple fields

In Django, Consider the following model class, class Report(models.Model): owner = models.ForeignKey( to=Owner, on_delete=models.CASCADE, related_name=’data’, ) balance = models.PositiveIntegerField() report_date = models.DateField() Assume this table has only the following four items, <QuerySet [{‘owner’: 1, ‘balance’: 100, ‘report_date’: datetime.date(2023, 3, 4)}, {‘owner’: 1, ‘balance’: 50, ‘report_date’: datetime.date(2023, 3, 9)}, {‘owner’: 2, ‘balance’: 1000, ‘report_date’: datetime.date(2023, 2,… Read More Django: Group by and return multiple fields

Find queryset where one field is greater than another one

I have model Shop, I want to get all shops which time_open(opening time) is more than time_closed(closing time). For example 23:00:00 is more than 07:00:00 (Shop_2(Night shift shop)). class Shop(models.Model): time_open = models.TimeField() time_closed = models.TimeField() for example i have two objects: shop_1 = { "time_open": "08:00:00", "time_closed": "22:00:00" } shop_2 = { "time_open": "23:00:00",… Read More Find queryset where one field is greater than another one

Django Q objects vs python code better performance?

What would provide better performance using filtering conditions with Q in django ORM or simply fetching unfiltered objects and comparing in python. employee_qs = employee.objects.filter(state=States.ACTIVE, topic_assn__topic_id=instance.ss_topic_id).select_related(‘c_data’).filter( Q(c_data__is_null=True) | Q(c_budget__gt=F(‘c_data__budget_spent’) + offset_amt)) V/s employee_qs = employee.objects.filter(state=States.ACTIVE, topic_assn__topic_id=instance.ss_topic_id).select_related(‘c_data’) for employee in employee_qs: if not employee.c_data or float(employee.budget)-employee.c_data.budget_spent > offset_amt: #do something… Which of these two choices would… Read More Django Q objects vs python code better performance?

How to fix delete record after some date in django not working?

I am trying to delete records after expiry_date_time is less than or equal to current date time but it was not working properly. Also giving this error RuntimeWarning: DateTimeField Question.date_time received a naive datetime (2022-08-28 10:15:19) while time zone support is active. API deleting wrong records. def delete(self, request): count = 0 count, _ =… Read More How to fix delete record after some date in django not working?

How to delete all records which is 24 hours old using django ORM?

Model: class Question(models.Model): question_text = models.CharField(max_length=100) option1 = models.CharField(max_length=50,null=False) option1_vote = models.IntegerField(default=0) option2 = models.CharField(max_length=50,null=False) option2_vote = models.IntegerField(default=0) option3 = models.CharField(max_length=50,null=False) option3_vote = models.IntegerField(default=0) option4 = models.CharField(max_length=50,null=False) option4_vote = models.IntegerField(default=0) date_time = models.DateTimeField(auto_now=True) user = models.ForeignKey(User,on_delete=models.CASCADE) We have the date_time field here. Question.objects.filter(condition).delete() what condition should I put there? >Solution : You can use Python… Read More How to delete all records which is 24 hours old using django ORM?

query foreignkey field from other field which is foreign key to other

I have model 3 models model 1 2 and 3 i need to access model 1 from model 3 model 2 has foreign key relation with model 1 and model 3 to model 2 how can access model 3 to model 1 class Record(models.Model): name = model.CharField(max_length=255) class Task(model.Model): name = models.CharField(‘Record’) record = models.ForeignKey(max_length,… Read More query foreignkey field from other field which is foreign key to other

How to Filter within a Filter in Django

I’m trying to filter category with age range. I want to see people with certain genders and then their age range. My models: from django.db import models # Create your models here. class catagory(models.Model): name = models.CharField(max_length=150) def __str__(self): return self.name class Age(models.Model): range = models.CharField(max_length=150) def __str__(self): return self.range class person(models.Model): name = models.CharField(max_length=100)… Read More How to Filter within a Filter in Django

How can I get all Django groups and optionally get associated user record?

This should be pretty simple since it’s very simple with pure SQL. I have the following query which gets exactly what I want in Django: SELECT auth_group.id, auth_group.name, auth_user.username FROM auth_group LEFT JOIN auth_user_groups ON auth_group.id = auth_user_groups.group_id LEFT JOIN auth_user ON auth_user_groups.user_id = auth_user.id WHERE auth_user.id = 3 OR auth_user.id IS NULL; How can… Read More How can I get all Django groups and optionally get associated user record?