django-orm

Django Queryset __in with None value in list

不想你离开。 提交于 2020-04-12 16:10:41
问题 a = M.objects.filter(f__in=[None, 1]) a.query.__str__() u'SELECT * FROM "app_m" WHERE "app_m"."f" IN (None, 1)' dont you think that would be IN (NULL, 1) ? like: a = M.objects.filter(f=None) a.query.__str__() u'SELECT * FROM "app_m" WHERE "app_m"."f" IS NULL' Is this a default SQL behavior, django bug or I am missing something with f__in= ? thank you in advance! 回答1: a = M.objects.filter(Q(f__isnull=True) | Q(f__in=['1',...])) 回答2: It seems to be and old bug in Django (https://code

select_related after prefetch_related

穿精又带淫゛_ 提交于 2020-04-08 18:09:46
问题 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: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 )

Django Annotated Query to Count all entities used in a Reverse Relationship

限于喜欢 提交于 2020-03-25 18:49:26
问题 This question is a follow up question for this SO question : Django Annotated Query to Count Only Latest from Reverse Relationship Given these models: class Candidate(BaseModel): name = models.CharField(max_length=128) class Status(BaseModel): name = models.CharField(max_length=128) class StatusChange(BaseModel): candidate = models.ForeignKey("Candidate", related_name="status_changes") status = models.ForeignKey("Status", related_name="status_changes") created_at = models.DateTimeField(auto

What is simplest way join __contains and __in?

匆匆过客 提交于 2020-01-21 17:26:13
问题 I am doing tag search function, user could observe a lot of tags, I get it all in one tuple, and now I would like to find all text which include at least one tag from the list. Symbolic: text__contains__in=('asd','dsa') My only idea is do loop e.g.: q = text.objects.all() for t in tag_tuple: q.filter(data__contains=t) For example: input tuple of tags, ('car', 'cat', 'cinema') output all messages what contains at least one word from that tuple, so My cat is in the car , cat is not allowed in

Django: why the manytomany choice box only has on side

社会主义新天地 提交于 2020-01-15 20:11:57
问题 I have extended the group model, where I added some manytomany fields, and in the admin page, it likes this: However, what I expected is this: Here is how I implemented the m2m field: class MyGroup(ProfileGroup): mobile = models.CharField(max_length = 15) email = models.CharField(max_length = 15) c_annotates = models.ManyToManyField(Annotation, verbose_name=_('annotation'), blank=True, null=True) c_locations = models.ManyToManyField(Location, verbose_name=_('locations'), blank=True, null=True

Django: return one filtered object per foreign key

霸气de小男生 提交于 2020-01-15 03:21:06
问题 Is it possible to return querysets that return only one object per foreign key? For instance, I want the to get the latest comments from django_comments, but I only want one comment (the latest comment) per object, i.e., only return the latest comment on an object and exclude all the past comments on that object. I guess this would be similar to a sql group_by on django_comments.content_type and django_comments.object_pk. ++ADDED INFO++ The end goal is to create a list of active comment

Translating query with JOIN expressions and a generic relation to Django ORM

无人久伴 提交于 2020-01-14 04:32:06
问题 class Business(models.Model): manager = models.ForeignKey(User, on_delete=models.CASCADE) #... class Event(models.Model): business = models.ForeignKey(Business, on_delete=models.CASCADE) text = models.TextField() when = models.DateTimeField() likes = GenericRelation('Like') class Like(models.Model): person = models.ForeignKey(User, on_delete=models.CASCADE) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = GenericForeignKey('content_type'

What is the usage of `FilteredRelation()` objects in Django ORM (Django 2.X)?

荒凉一梦 提交于 2020-01-14 02:55:13
问题 I've seen Django 2.0 consists of FilteredRelation object in queryset. What is the usage of newly introduced FilteredRelation ? What I've looked into? I observed Django 2.0 Documentation but I could not understand idea behind this FilteredRelation object. I looked into following code. But I didn't get it. >>> from django.db.models import FilteredRelation, Q >>> Restaurant.objects.annotate( ... pizzas_vegetarian=FilteredRelation( ... 'pizzas', condition=Q(pizzas__vegetarian=True), ... ), ... )