Querying n-items based on a column - django

These are my models:
from django.db import models
class A(models.Model):
# fields
class B(models.Model):
a = models.ForeignKey(A)
# fields
I have some items from model A:
items = A.objects.filter(some_column=some_value)
Now I want 2 model B objects for each object in items. If there are 5 objects in items then I want total 10 objects from model B, 2 model B objects for each object of model A. Hope I made my requirement clear. I tried some queries, but ended up with querying model B for each model A object.
Also the solution should be well optimized, I would like to avoid 20 different queries for 20 objects in items.
If it is not possible with ORM, then I can use raw query as well.

you can get those using related query and prefetch_related
like
items = A.objects.prefetch_related('b_set').filter(some_column=some_value)
for item in items:
/* Here you get all modal B object for particular item */
obj_of_modal_B = item.b_set.all() # Here b is model name in small
you can also overwrite related_query name using related_name
class A(models.Model):
# fields
class B(models.Model):
a = models.ForeignKey(A,related_name='custom_name')
# fields
and then use like
items = A.objects.prefetch_related('custom_name').filter(some_column=some_value)
for item in items:
/* Here you get all modal B object for particular item */
obj_of_modal_B = item.custom_name.all()

Use prefecth_related. It won't query in for loop. It will have two query only
a = A.objects.prefetch_related('b')
Read about prefetch_related in docs for more detailed information
https://docs.djangoproject.com/en/3.0/topics/db/queries/

Related

Is it possible to link multiple models to one fiel in django?

Let's say I have these models:
class Material(models.Model):
name = models.CharField([...])
class Consumable(models.Model):
name = models.CharField([...])
restores = models.IntegerField([...])
class Weapon(models.Model):
name = models.CharField([...])
damage = models.IntegerField([...])
# And then I have an 'inventory', like this one:
class Inventory(models.Model):
user = models.ForeignKey([...]) # to which user you want to link the item
item = models.ForeignKey([...]]) # which item
quantity = models.IntegerField([...]) # how many of it
I want to be able to have all Material, Consumable, and Weapon models listed in the 'item' field, so when you want to add an item as an inline, you would see all 3 models' objects.
Something like
# instead of this
item = models.ForeignKey(Consumable) # which item
# want something like this
item = models.ForeignKey(Consumable and Material and Weapon) # which item
# this wouldn't work ofc...
Is there a way to collect all 3 of them and pass them to the 'item' field, without the need of restarting the server? (when making a "choices" list that queries from a model you must restart the server to see the newly added objects, I don't want that.)
I also want to stick to the built-in admin of Django since it provided everything I need for the past couple of months, but I am open to any ideas.
I could be wrong but I think you are making this more complex than it needs to be. Instead of doing separate classes for materials (type of material) and consumable (type of product), you can have that built in the last class as model field as category or bolean fields.
class Products(models.Model):
material_type =
consumable = boolean for yes no or you can do multiple choice field
Then for items you can query the number of items based on material_type or consumable model fields (see query filters for for more).
all_items = Products.model.all()
consumable_items = Products.model.filter(your filter logic goes here)
Hope this helps!

Checking for many-to-many relation OR a property

How can I check whether a many-to-many relationship exists or another property is fulfilled? When I try the query, it returns some rows twice!
Given a model
from django.db import models
class Plug(models.Model):
name = models.CharField(primary_key=True, max_length=99)
class Widget(models.Model):
name = models.CharField(primary_key=True, max_length=99)
shiny = models.BooleanField()
compatible = models.ManyToManyField(Plug)
I have the following items in my database:
from django.db.models import Q
schuko = Plug.objects.create(name='F')
uk = Plug.objects.create(name='G')
Widget.objects.create(name='microwave', shiny=True).compatible.set([uk])
Widget.objects.create(name='oven', shiny=False).compatible.set([uk])
Widget.objects.create(name='pc', shiny=True).compatible.set([uk, schuko])
Now I want all names of widgets that are shiny and/or compatible with Schuko:
shiny_or_schuko = sorted(
Widget.objects.filter(Q(shiny=True) | Q(compatible=schuko))
.values_list('name', flat=True))
But to my surprise, this does not return ['microwave', 'pc']. Instead, 'pc' is listed twice, i.e. shiny_or_schuko is ['microwave', 'pc', 'pc'].
Is this a Django bug? If not, how can I set up the query that I get 'pc' just once?
Is this a Django bug?
No. You simply perform a LEFT OUTER JOIN with the many-to-many table. If two or more related objects match, it will be included multiple times. This can be wanted behavior, for example if you add extra annotations to the elements that takes values from these related objects.
You can make use of .distinct() [Django-doc] to return only distinct elements:
Widget.objects.filter(
Q(shiny=True) | Q(compatible=schuko)
).values_list('name', flat=True).distinct()

filter with select_related on Django

I have a problem with the usage of select_related feature of Django with the filter operation, here's my problem, I have three classes :
class A:
# various fields here
class B(models.model):
related_A = models.ForeignKey(A)
related_C = models.ForeignKey(C)
attribute1 = models.CharField(..)
# Other attributes
class C(models.model):
# Attributes
What I'm trying to do is, getting class A by filtering on class B on the key related_C according to another parameter attribute1 (from class B).
To illustrate it properly, I have a function get_class_A(self) in my class C
get_class_A(self,param):
classes_B = B.objects.filter(related_C = self,attribute1 = param)
It returns a QuerySet of classes B. What I want to do is to follow the ForeignKey pointing to A in order to transform this QuerySet of B into a list of objects A.
I tried various things such as :
classes_A = B.objects.select_related('A').filter(related_C = self, attribute1 = param)
and some variations but nothing worked. Does anyone knows how to do this ?
Thanks
def get_class_A(self, param):
return A.objects.filter(b__related_c=self, b__attribute1=param).distinct()
What you've described looks a lot like a ManyToMany relationship between A and C. If you declare it as such, and include your extra attributes by specifying B as a through model, Django will create the relationship between A and C for you.
Also, select_related() has nothing to do with filtering results, it's just a tool that can allow you to reduce the number of database queries. From the docs:
This is a performance booster which results in a single more complex query but means later use of foreign-key relationships won’t require database queries.

Ordering many-to-many relations in Django models

Suppose you have a many-to-many relation in a Django model, such as:
class GroceryList(models.Model):
items = models.ManyToManyField(GroceryItem, related_name='in_lists')
class GroceryItem(models.Model):
name = models.CharField(unique=True)
You and me can both have the same items in two different lists, such as Avocado, and they will point to the same Avocado object.
What is the best way to implement an arbitrary order for items in each list, that can be edited separately for each list? (i.e. I have Avocado first in my list, while you have it at index 4)
django-ordered-model seems like an interesting solution, but it assumes global order across all objects.
You can use the intermediate table using through and add the ordered field in that table.
class GroceryList(models.Model):
items = models.ManyToManyField(GroceryItem, related_name='in_lists',
through='Order')
class GroceryItem(models.Model):
name = models.CharField(unique=True)
class Order(models.Model):
number = models.PositiveIntegerField()
gl = models.ForeignKey(GroceryList)
gi = models.ForeignKey(GroceryItem)
So instead of doing grocerylist.items.add(groceryitem) you can do
#for groceryitem1 as 1st item in grocerylist1
Order.objects.create(gl=grocerylist1, gi=groceryitem1, number=1)
#for groceryitem1 as 10th item in grocerylist2
Order.objects.create(gl=grocerylist2, gi=groceryitem1, number=10)

How to get object from manytomany?

I have models:
class Z(models.Model):
name = ...
class B(model.Model):
something = model...
other = models.ForeignKey(Z)
class A(models.Model):
date = model.DateTimeField()
objs_b = models.ManyToManyField(B)
def get_obj_b(self,z_id):
self.obj_b = self.objs_b.get(other=z_id)
and query:
qs = A.objects.filter(...)
but if I want get object B related to A I must call get_obj_b:
for item in gs:
item.get_obj_b(my_known_z_id)
It was generate many queries. How to do it simple? I can not change models, and generally I must use filter (not my own manager) function.
If you are using Django 1.4, I would suggest that you use prefetch_related like this:
A.objects.all().prefetch_related('objs_b__other')
This would minimize the number of queries to 2: one for model A, and one for 'objs_b' joined with 'other'
And you can combine it with a filter suggested by pastylegs:
A.objects.filter(objs_b__other__id=z_id).prefetch_related('objs_b__other')
For details see: https://docs.djangoproject.com/en/1.4/ref/models/querysets/#prefetch-related