django-queryset

select_related after prefetch_related

牧云@^-^@ 提交于 2020-04-08 18:08:26
问题 My Models looks like: class Book(models.Model): publisher = models.ForeignKey(Publisher) # This is not important class Baz(models.Model): a = models.CharField(max_length=100) class Page(models.Model): book = models.ForeignKey(Book) baz = models.ForeignKey(Baz) And I am trying to run a query like this: [[x.baz.a for x in y.page_set.all()] for y in Book.objects.all().prefetch_related('page_set', 'page_set__baz')] Which I think the ORM should be able to perform as two queries: ( Page JOIN Baz )

select_related after prefetch_related

我的未来我决定 提交于 2020-04-08 18:06:43
问题 My Models looks like: class Book(models.Model): publisher = models.ForeignKey(Publisher) # This is not important class Baz(models.Model): a = models.CharField(max_length=100) class Page(models.Model): book = models.ForeignKey(Book) baz = models.ForeignKey(Baz) And I am trying to run a query like this: [[x.baz.a for x in y.page_set.all()] for y in Book.objects.all().prefetch_related('page_set', 'page_set__baz')] Which I think the ORM should be able to perform as two queries: ( Page JOIN Baz )

How can I return a queryset for a fk or m2m field in wagtail admin create form?

醉酒当歌 提交于 2020-03-26 03:06:05
问题 My question holds for any wagtail panel that returns select options from a foreignkey or M2M relationship, like a PageChooserPanel or SnippetChooserPanel. How can I filter the list of options server-side, for the user to only see the information he has access to? I have a Plan model. When creating a new plan, I would like request.user to only see the list of companies he is affiliated to. from modelcluster.models import ClusterableModel from wagtailautocomplete.edit_handlers import

Django Exists() / ~Exists() return if there is no matching data?

五迷三道 提交于 2020-03-25 18:56:13
问题 EDIT: As per schillingt's answer below I have switched to using Case/When: context['db_orders'] = Order.objects.filter( retailer_code=self.object.retailer_code).annotate( in_db=Case(When(Q(Subquery(self.object.suppliers.filter( supplier_code=(OuterRef('supplier_code'))) ), then=Value(True), default=Value(False), output_field=NullBooleanField())))) However I'm now struggling with an errror: FieldError at /retailers/A001/ Cannot resolve expression type, unknown output_field Original question: I

How to annotate a queryset with number of days since creation

笑着哭i 提交于 2020-03-03 13:31:04
问题 I'm trying to do some complex ordering based on promotions: an article is promoted every 7th day since creation (the articles expire after 30 days). My approach is to annotate the queryset with the number of days since it was created, but the value of the annotated field ( days_since_creation ), in my code, is always 0. from datetime import timedelta from django.test import TestCase from django.db.models import ExpressionWrapper, F from django.db.models.fields import IntegerField from django

How to annotate a queryset with number of days since creation

江枫思渺然 提交于 2020-03-03 13:29:01
问题 I'm trying to do some complex ordering based on promotions: an article is promoted every 7th day since creation (the articles expire after 30 days). My approach is to annotate the queryset with the number of days since it was created, but the value of the annotated field ( days_since_creation ), in my code, is always 0. from datetime import timedelta from django.test import TestCase from django.db.models import ExpressionWrapper, F from django.db.models.fields import IntegerField from django

How to annotate a queryset with number of days since creation

大憨熊 提交于 2020-03-03 13:28:26
问题 I'm trying to do some complex ordering based on promotions: an article is promoted every 7th day since creation (the articles expire after 30 days). My approach is to annotate the queryset with the number of days since it was created, but the value of the annotated field ( days_since_creation ), in my code, is always 0. from datetime import timedelta from django.test import TestCase from django.db.models import ExpressionWrapper, F from django.db.models.fields import IntegerField from django

How to annotate a queryset with number of days since creation

ぐ巨炮叔叔 提交于 2020-03-03 13:27:13
问题 I'm trying to do some complex ordering based on promotions: an article is promoted every 7th day since creation (the articles expire after 30 days). My approach is to annotate the queryset with the number of days since it was created, but the value of the annotated field ( days_since_creation ), in my code, is always 0. from datetime import timedelta from django.test import TestCase from django.db.models import ExpressionWrapper, F from django.db.models.fields import IntegerField from django

Django reverse query by the last created object

半城伤御伤魂 提交于 2020-02-22 09:23:48
问题 I have two models: class SomeActivity(models.Model): name = models.ChartField(max_length=100) class SomeStatus(models.Model): name = models.CharField(max_length=100) status = models.IntegerField(choises=STATUS_CHOISES) some_activity = models.ForeignKey(SomeActivity, related_name='statuses') The last created status for the activity is the current one. To get it I use this code: try: last_status = some_activity.statuses.latest('id') except: last_status = None But the problem is when I want to

Django reverse query by the last created object

北战南征 提交于 2020-02-22 09:21:53
问题 I have two models: class SomeActivity(models.Model): name = models.ChartField(max_length=100) class SomeStatus(models.Model): name = models.CharField(max_length=100) status = models.IntegerField(choises=STATUS_CHOISES) some_activity = models.ForeignKey(SomeActivity, related_name='statuses') The last created status for the activity is the current one. To get it I use this code: try: last_status = some_activity.statuses.latest('id') except: last_status = None But the problem is when I want to