django-orm

Django ORM, sum of multiple columns

萝らか妹 提交于 2019-12-18 07:21:09
问题 I have a question about how we can filter by SUM of multiple columns. Example: class Foo(models.Model): i1 = models.IntegerField() i2 = models.IntegerField() i3 = models.IntegerField() And I need to filter objects where SUM of i1, i2, i3 is less then 200. I've tried achive it with: Foo.objects.agregate(i_sum=Sum(i1,i2,i3)).filter(i_sum__lt=200) # error Foo.objects.agregate(i_sum=Sum([i1,i2,i3])).filter(i_sum__lt=200) # error Thanks. 回答1: You can use F(), and with annotation: Foo.objects

Django ORM, sum of multiple columns

邮差的信 提交于 2019-12-18 07:20:09
问题 I have a question about how we can filter by SUM of multiple columns. Example: class Foo(models.Model): i1 = models.IntegerField() i2 = models.IntegerField() i3 = models.IntegerField() And I need to filter objects where SUM of i1, i2, i3 is less then 200. I've tried achive it with: Foo.objects.agregate(i_sum=Sum(i1,i2,i3)).filter(i_sum__lt=200) # error Foo.objects.agregate(i_sum=Sum([i1,i2,i3])).filter(i_sum__lt=200) # error Thanks. 回答1: You can use F(), and with annotation: Foo.objects

Django ORM, group by day

偶尔善良 提交于 2019-12-18 03:02:10
问题 I am trying to group products by DAY, however date_created is a datetime field. Product.objects.values('date_created') \ .annotate(available=Count('available_quantity')) returns: [ {'date_created': datetime.datetime(2012, 4, 14, 13, 3, 6), 'available': 1}, {'date_created': datetime.datetime(2012, 4, 14, 17, 12, 9), 'available': 1}, ... ] I want: [ {'date_created': datetime.datetime(2012, 4, 14), 'available': 2}, ... ] edit: database backend MYSQL 回答1: Inspired by this question try this for

Django: List field in model?

 ̄綄美尐妖づ 提交于 2019-12-17 15:26:46
问题 In my model, I want a field that has a list of triplets. e.g. [[1, 3, 4], [4, 2, 6], [8, 12, 3], [3, 3, 9]] . Is there a field that can store this data in the database? 回答1: You can convert it into string by using JSON and store it as string. For example, In [3]: json.dumps([[1, 3, 4], [4, 2, 6], [8, 12, 3], [3, 3, 9]]) Out[3]: '[[1, 3, 4], [4, 2, 6], [8, 12, 3], [3, 3, 9]]' You can add a method into your class to convert it automatically for you. import json class Foobar(models.Model): foo =

Django: ManyToMany filter matching on ALL items in a list

梦想的初衷 提交于 2019-12-17 09:49:53
问题 I have such a Book model: class Book(models.Model): authors = models.ManyToManyField(Author, ...) ... In short: I'd like to retrieve the books whose authors are strictly equal to a given set of authors. I'm not sure if there is a single query that does it, but any suggestions will be helpful. In long: Here is what I tried, (that failed to run getting an AttributeError) # A sample set of authors target_authors = set((author_1, author_2)) # To reduce the search space, # first retrieve those

How to create an object for a Django model with a many to many field?

守給你的承諾、 提交于 2019-12-17 04:38:06
问题 My model: class Sample(models.Model): users = models.ManyToManyField(User) I want to save both user1 and user2 in that model: user1 = User.objects.get(pk=1) user2 = User.objects.get(pk=2) sample_object = Sample(users=user1, users=user2) sample_object.save() I know that's wrong, but I'm sure you get what I want to do. How would you do it ? 回答1: You cannot create m2m relations from unsaved objects. If you have the pk s, try this: sample_object = Sample() sample_object.save() sample_object.users

What's the difference between select_related and prefetch_related in Django ORM?

梦想与她 提交于 2019-12-17 02:02:09
问题 In Django doc, select_related() "follows" foreign-key relationships, selecting additional related-object data when it executes its query. prefetch_related() does a separate lookup for each relationship, and does the "joining" in Python. What does it mean by "doing the joining in python"? Can someone illustrate with an example? My understanding is that for foreign key relationship, use select_related ; and for M2M relationship, use prefetch_related . Is this correct? 回答1: Your understanding is

Exclude field from values() or values_list()

跟風遠走 提交于 2019-12-14 01:23:14
问题 Is there an efficient way to exclude fields from the function values() or values_list . e.g Videos.objects.filter(id=1).get().values() I want to exclude from this queryset the field duration . I know that I can specify fields what I want to have in the result but what if I want everything but only one field not. Like in the cases if I have 20 fields and if I want only one from them not. Thanks 回答1: You must use defer This will not add defined fields to your select query. Videos.objects.filter

GeoDjango query: all point that are contained into a multi polygon

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 15:35:17
问题 I have two models: Model_A that contains a GeoDjango Point; Model_B that contains a GeoDjnago MultiPololygon; For every element in Model_A I have to check if the point is contained into some m_polygon of Model_B element; I'm able to make this simple query. But I also thought: I have a lot of elements in Model_A and few elements in Model_B. So, probably is more efficient to iterate all elements in Model_B and check if exist some element in Model_A that it is contained into the current Model_B

Can Django's ORM return a nested object queryset?

孤街醉人 提交于 2019-12-13 07:39:48
问题 If I have two models in a many to many relationship like below: class Topping(models.Model): name = models.CharField(max_length=50) class Pizza(models.Model): name = models.CharField(max_length=50) toppings = models.ManyToManyField(Topping) Can I create a queryset that will give me something like this?: [ { "name": "Hawaiian", "toppings": [ {"name": "Pineapple"}, {"name": "Canadian Bacon"}, {"name": "Cheese"} ] }, { "name": "Pepperoni Pizza", "toppings": [ {"name": "Pepperoni"}, {"name":