django-orm

Django Filter by ForeignKey value

Django Filter by ForeignKey value Question: Suppose I have two models class DocumentType(BaseModel): name = models.CharField(max_length=128) code = models.CharField(max_length=128) exp_days = models.PositiveIntegerField("Remind me before (Days)", blank=True, null=True) def __str__(self): return str(self.name) class Document(BaseModel): type = models.ForeignKey("masters.DocumentType", on_delete=models.PROTECT) date_of_expiry = models.DateField(blank=True, null=True) attachment = models.FileField(blank=True, null=True) def __str__(self): return str(self.employee) How can I retrieve documents with …

Total answers: 1

Django Q objects vs python code better performance?

Django Q objects vs python code better performance? Question: 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 > …

Total answers: 1

When I do "makemigrations" I get do "makemigrations"

When I do "makemigrations" I get do "makemigrations" Question: I removed all migration files from my django project and now when i want to re-create i get this ./manage.py makemigrations INFO: AXES: BEGIN LOG :: [axes.apps] 2022-09-15 16:51:59,923 – /home/mixnosha/work_MVD/MVD_LTP/MVD_LTP-ltp/venv/lib/python3.10/site- packages/axes/apps.py:33 INFO: AXES: Using django-axes version 5.31.0 :: [axes.apps] 2022-09-15 16:51:59,926 – /home/mixnosha/work_MVD/MVD_LTP/MVD_LTP-ltp/venv/lib/python3.10/site- packages/axes/apps.py:34 INFO: …

Total answers: 1

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

How to delete all records which is 24 hours old using django ORM? Question: 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 …

Total answers: 2

Calculate sum of model objects in django serializers

Calculate sum of model objects in django serializers Question: In my app, each user has his wallets in which he can save his daily expenses. How can I get sum of money for each instance of wallet with many expenses saved to it? I’ve tried serializers.SerializerMethodField() from django.db.models import Sum from django.db.models import Q class …

Total answers: 2

Order Django Queryset by attribute that could be char or int?

Order Django Queryset by attribute that could be char or int? Question: I have the following model that I need to order by the grade attribute, but that attribute can be either a character or integer (K, KA, KF, NA, 1, 2, 3, etc.)… what is the best way to accomplish that? We are using …

Total answers: 1

How to make (if & else) for internal values in django orm?

How to make (if & else) for internal values in django orm? Question: I am making models for my store project and I wanted to know why the code I wrote is wrong? And how can I write correctly? I want only the time when a product is sold from me to be recorded in …

Total answers: 1

Auto increment rank field in my response in Django Rest Framework

Auto increment rank field in my response in Django Rest Framework Question: I’ve a StudentAnswer model which stores the answer id given by the student(User) in a quiz. If the answer is correct then 1 marks else 0. The model looks like this: class StudentAnswer(models.Model): user = models.ForeignKey(User, related_name=’user_question_answer’, on_delete=models.SET_NULL,null=True, blank=True) answer = models.ForeignKey(QuizQuestionAnswer, related_name=’user_answer’, …

Total answers: 1

Django : Ignore a __in type filter in the queryset if it searches the empty list

Django : Ignore a __in type filter in the queryset if it searches the empty list Question: Having this code sequence: queryset = self.filter( brand_id__in=( UserObjectPermission.objects.filter( content_type=brand_ctype, user=user, ).values_list(‘object_pk’) ) ) If there is no UserObjectPermission object that matches the filter content_type=brand_ctype, user=user then the end result will be empty queryset, because brand_id __in will …

Total answers: 2

How to apply filter on django ManyToManyField so that multiple value of the field follow the condition?

How to apply filter on django ManyToManyField so that multiple value of the field follow the condition? Question: class Publication(models.Model): title = models.CharField(max_length=30) class Article(models.Model): headline = models.CharField(max_length=100) publications = models.ManyToManyField(Publication) p1 = Publication.objects.create(title=’The Python Journal’) p2 = Publication.objects.create(title=’Science News’) p3 = Publication.objects.create(title=’Science Weekly’) I want to filter articles, published in both p1 and p3. …

Total answers: 3