django-queryset

Django query single underscore behaving like double underscore?

此生再无相见时 提交于 2020-06-25 21:55:31
问题 I made a typo in my code recently and noticed that I got the same behavior, so I was wondering what the difference between single and double underscores are in django queries. >>> underscore = MyModel.objects.filter(foreign_key_id=var) >>> double_underscore = MyModel.objects.filter(foreign_key__id=var) >>> underscore == double_underscore False >>> list(underscore) == list(double_underscore) True I'm not sure what equality method is being used to compare the querysets, but when I convert to

Django query single underscore behaving like double underscore?

两盒软妹~` 提交于 2020-06-25 21:55:18
问题 I made a typo in my code recently and noticed that I got the same behavior, so I was wondering what the difference between single and double underscores are in django queries. >>> underscore = MyModel.objects.filter(foreign_key_id=var) >>> double_underscore = MyModel.objects.filter(foreign_key__id=var) >>> underscore == double_underscore False >>> list(underscore) == list(double_underscore) True I'm not sure what equality method is being used to compare the querysets, but when I convert to

nested query filter _ Django

余生长醉 提交于 2020-06-17 02:12:05
问题 I keep it simple. I have 3 models. class C(models.model): some_field = models.BooleanField(default=False) class B(models.model): b = models.ForeignKey(C) class A(models.model): a = models.ForeignKey(B) I need a query filter that gets A.a.b.some_field = True. how can I achieve this ? 回答1: You can filter your A objects that satisfy this condition with: A.objects.filter( a__b__some_field=True ) This will generate a query that looks, more or less like: SELECT a.* FROM a JOIN b ON a.a_id = b.id

nested query filter _ Django

独自空忆成欢 提交于 2020-06-17 02:11:25
问题 I keep it simple. I have 3 models. class C(models.model): some_field = models.BooleanField(default=False) class B(models.model): b = models.ForeignKey(C) class A(models.model): a = models.ForeignKey(B) I need a query filter that gets A.a.b.some_field = True. how can I achieve this ? 回答1: You can filter your A objects that satisfy this condition with: A.objects.filter( a__b__some_field=True ) This will generate a query that looks, more or less like: SELECT a.* FROM a JOIN b ON a.a_id = b.id

Order Django Query Results by Foreign Key

送分小仙女□ 提交于 2020-05-26 10:17:06
问题 I have a model that is set up as follows: class Log(models.Model): name = models.ForeignKey(User) date = models.DateField() time = models.TimeField() I know this does not work, but is there any other way I can run a query something like this: Logs.objects.filter(date=someDate).order_by('name__last_name') I just need the final result to be a QuerySet ordered by the last name of the User that is related by the ForeignKey . I'm really at my wits end about this one. Anything would help: some

manually create a Django QuerySet or rather manually add objects to a QuerySet

天涯浪子 提交于 2020-05-24 17:27:26
问题 Basically I need a graceful way to do the following:- obj1 = Model1.objects.select_related('model2').get(attribute1=value1) obj2 = Model1.objects.select_related('model2').get(attribute2=value2) model2_qs = QuerySet(model=Model2, qs_items=[obj1.model2,obj2.model2]) I may not be thinking right, but doing something like the following seems infinitely stupid to me.: - obj1 = Model1.objects.select_related('model2').get(attribute1=value1) model2_qs = Model2.objects.filter(pk=obj1.model2.pk) Yes, I

Django. How to annotate a object count from a related model

不羁岁月 提交于 2020-05-15 02:15:46
问题 lets say I have a model Comments and another model Answers. I want to query all comments but also include in the query the number of answers each comment has. I thought annotate() would be useful in this case, but I just don't know how to write it down. The docs write that: New in Django 1.8: Previous versions of Django only allowed aggregate functions to be used as annotations. It is now possible to annotate a model with all kinds of expressions. So. This is an example of my models: class

django order by count of many to many object

不打扰是莪最后的温柔 提交于 2020-05-12 20:33:06
问题 I have a model : class Category(models.Model): questions = models.ManyToManyField('Question', related_name="cat_question", blank=True) cat_name = models.CharField(max_length=50) cat_description = models.CharField(max_length=255) def __unicode__(self): return self.cat_name and in my view I have : top_categories = Category.objects.all()[:7]. I am passing it as context in the template. I want to sort the top categories by the number of questions in it like if A category has most question then it

django order by count of many to many object

梦想与她 提交于 2020-05-12 20:31:32
问题 I have a model : class Category(models.Model): questions = models.ManyToManyField('Question', related_name="cat_question", blank=True) cat_name = models.CharField(max_length=50) cat_description = models.CharField(max_length=255) def __unicode__(self): return self.cat_name and in my view I have : top_categories = Category.objects.all()[:7]. I am passing it as context in the template. I want to sort the top categories by the number of questions in it like if A category has most question then it

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 )