Additional Foreign Key Query in Django - django

I am a newbie to django and was reading about select_related. I understand that whenever a foreign key is accessed django executes an additional query. But when I checked with DEBUG log in my code, it seems django executes two queries no matter if the foreign key is accessed or not. Can someone explain this behaviour ?
class Person(models.Model):
# ...
name = models.CharField(max_length=250)
class Book(models.Model):
# ...
author = models.ForeignKey(Person, on_delete=models.CASCADE)
As per doc
# Without select_related()...
b = Book.objects.get(id=4) # Executes a query.
p = b.author #Executes a query.
But with the get() it executes two queries
b = Book.objects.get(id=4) # Executes two queries (one for books one for author).

First of all, you need to call select select_related:
ids = [1,2,3,4]
query = Book.objects.filter(id__in=ids).select_related('author')
notice that I did that using the filter method and not the get method.
the reason is that select/prefetch related doesn't work with the get method.
if you still want only one object with select related you should do:
book = Book.objects.filter(id=4).select_related('author')[0]
author = book.author
or do:
book = Book.objects.select_related('author').get(id=4)
author = book.author
if you want to do it for multiple objects and get all the authors:
ids = [1,2,3,4]
query = Book.objects.filter(id__in=ids).select_related('author')
authors_in_query = [book.author for book in query]

Related

Django - Getting Related objects

There are such models:
class Nomenclature(models.Model):
nameNom = models.CharField(max_length=150,verbose_name = "Название номеклатуры")
numNom = models.CharField(max_length=50,verbose_name = "Номер номеклатуры",unique=True)
quantity = models.IntegerField(verbose_name="Количество", default=0)
numPolk = models.CharField(max_length=150,verbose_name = "Номер полки/места"
class Changes(models.Model):
numNomenclature = models.ForeignKey(Nomenclature, on_delete=models.CASCADE,related_name="chamges",verbose_name="Номер номеклатуры")
quantity = models.IntegerField(verbose_name="Количество",null=True)
location = models.CharField(max_length=50,verbose_name = "Место установки")
fullname = models.CharField(max_length=150,verbose_name = "ФИО")
appointment = models.CharField(max_length=50,verbose_name = "Назначение")
created_at = models.DateTimeField(auto_now_add=True,verbose_name='Дата/время', null=True)
It is necessary to output the name and number of the nomenclature and all related changes to the template, and also output all fields
I found that select_related exists, but I thought that it doesn't work the way I need it to.
I'm not completely sure if this is what you need.
If you need to fetch all of the changes, from a single "Nomenclature" model:
md = Nomenclature.objects.get(id=id) # Not sure how you fetch this, just an example.
all_changes_for_md = Changes.objects.filter(numNomenclature__id=md.id)
This will fetch you all changes for a nomenclature model.
Also possible to do it like this:
md = Nomenclature.objects.get(id=id) # Not sure how you fetch this, just an example.
all_changes_for_md = md.chamges.all() # You made a typo in the related name.
Select related has another purpose, it is used for prefetching.
From the Django docs:
select_related(*fields)
Returns a QuerySet that will “follow” foreign-key relationships, selecting additional related-object data when it executes its query. 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.
https://docs.djangoproject.com/en/4.1/ref/models/querysets/#select-related

Django query joining two tables

I am new in django. I want to create a query in django I tried with select_related but I don't know how to insert the second part of the condition: AND model1.qty >= model2.items
I've tried:
Model1.objects.select_related('model2).filter(model1.qty__gte=?)
But it's not working properly.
Below is the SQL query which I want to implement with django queryset:
SELECT model1.name,model2.name WHERE model1.id=model2.model1.id AND model1.qty >= model2.items
My models:
class Article(models.Model):
date_crea = models.DateTimeField('Créer le', auto_now_add=True)
designation = models.TextField('designation', max_length=500)
seuil = models.IntegerField('Seuil d\'alerte')
class Stock(models.Model):
date_crea = models.DateTimeField('Créer le', auto_now_add=True)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
qte_reel = models.IntegerField('stock reel',default=0)
You use an F expression to refer to the value of a field in the database. There have to be relations to follow from the current object to the field on the object that you want to compare against.
I'm not clear how the question relates to the posted models, but an F expression can follow a foreign key so
Stock.objects.filter( qte_reel__gte = F( 'article__seuil' ))
would work, if those fields are the ones you want to compare.

Reverse select_related for onetoone field not working

According to the docs then you should be able to catch reverse relations when those relations are onetoone with a select_related(). But it is not working, so what could I be missing?
My class looks like this:
class MainPage(models.Model):
book = models.OneToOneField(Book, primary_key=True)
text = models.TextField(blank=True)
I can do this fine:
book = Book.objects.get(id=book_id, active=True)
main_page = book.mainpage
But doing like so does not lower the database calls:
book = Book.objects.select_related('mainpage').get(id=book_id, active=True)
main_page = book.mainpage
I guess you intend to hit only a single sql query (using one to one join). This might work for that:
book = Book.objects.get(mainpage__book_id=book_id, active=True)
EDIT:
The query in your question does not work because select_related works only with the querysets while .get returns an instance object. Thus this should work:
book = Book.objects.select_related('mainpage').filter(id=book_id, active=True)[0]
main_page = book.mainpage

Q objects and the '&' operator in django

I have a curious problem.
I have 3 objects. All the same
class Articles(models.Model):
owner = models.ForeignKey(Author)
tags = models.ManyToManyField('Tag')
class Tag(models.Model):
name = models.CharField(max_length=255)
and so I have 3 Articles. With all the same tags: 'tag1' and 'tag2'
And I have queries
actionsAll = Articles.objects.filter((Q(tags__name__exact="tag1") | Q(tags__name__exact="tag2"))).distinct()
This gives me all my articles. It will return 6 articles w/o distinct() since it will collect each article 2x since they have both tags.
However with this query:
actionsAll = Articles.objects.filter((Q(tags__name__exact="tag1") & Q(tags__name__exact="tag2"))).distinct()
This gives me no articles.
Since the articles contain both the tags, it should return them all shouldnt it?
If you look at the SQL it generates, you'll see that it checks to see if the same tag has both names. What you need is an IN query or an EXISTS query that traverses the relation.
** import Q from django
from *models import SuperUser, NameUser
import operator
# we do not know the name in the superhero
super_users = SuperUser.objects.all()
q_expressions = [Q(username=user.username) for user in super_users]
# we have bind super_hero with user
name_superheroes_qs = models.NameUser.objects.filter(reduce(operator.or_, q_expressions))

Fetching ManyToMany objects from multiple objects through intermediate tables

Is there an easy way to fetch the ManyToMany objects from a query that returns more than one object? The way I am doing it now doesn't feel as sexy as I would like it to. Here is how I am doing it now in my view:
contacts = Contact.objects.all()
# Use Custom Manager Method to Fetch Each Contacts Phone Numbers
contacts = PhoneNumber.objects.inject(contacts)
My Models:
class PhoneNumber(models.Model):
number = models.CharField()
type = models.CharField()
# My Custom Manager
objects = PhoneNumberManager()
class Contact(models.Model):
name = models.CharField()
numbers = models.ManyToManyField(PhoneNumber, through='ContactPhoneNumbers')
class ContactPhoneNumbers(models.Model):
number = models.ForeignKey(PhoneNumber)
contact = models.ForeignKey(Contact)
ext = models.CharField()
My Custom Manager:
class PhoneNumberManager(models.Manager):
def inject(self, contacts):
contact_ids = ','.join([str(item.id) for item in contacts])
cursor = connection.cursor()
cursor.execute("""
SELECT l.contact_id, l.ext, p.number, p.type
FROM svcontact_contactphonenumbers l, svcontact_phonenumber p
WHERE p.id = l.number_id AND l.contact_id IN(%s)
""" % contact_ids)
result = {}
for row in cursor.fetchall():
id = str(row[0])
if not id in result:
result[id] = []
result[id].append({
'ext': row[1],
'number': row[2],
'type': row[3]
})
for contact in contacts:
id = str(contact.id)
if id in result:
contact.phonenumbers = result[id]
return contacts
There are a couple things you can do to find sexiness here :-)
Django does not have any OOTB way to inject the properties of the through table into your Contact instance. A M2M table with extra data is a SQL concept, so Django wouldn't try to fight the relations, nor guess what should happen in the event of namespace collision, etc... . In fact, I'd go so far as to say that you probably do not want to inject arbitrary model properties onto your Contact object... if you find yourself needing to do that, then it's probably a sign you should revise your model definition.
Instead, Django provides convenient ways to access the relation seamlessly, both in queries and for data retrieval, all the while preserving the integrity of the entities. In this case, you'll find that your Contact object offers a contactphonenumbers_set property that you can use to access the through data:
>>> c = Contact.objects.get(id=1)
>>> c.contactphonenumbers_set.all()
# Would produce a list of ContactPhoneNumbers objects for that contact
This means, in your case, to iterate of all contact phone numbers (for example) you would:
for contact in Contact.objects.all():
for phone in contact.contactphonenumbers_set.all():
print phone.number.number, phone.number.type, phone.ext
If you really, really, really want to do the injection for some reason, you'll see you can do that using the 3-line code sample immediately above: just change the print statements into assignment statements.
On a separate note, just for future reference, you could have written your inject function without SQL statements. In Django, the through table is itself a model, so you can query it directly:
def inject(self, contacts):
contact_phone_numbers = ContactPhoneNumbers.objects.\
filter(contact__in=contacts)
# And then do the result construction...
# - use contact_phone_number.number.phone to get the phone and ext
# - use contact_phone_number.contact to get the contact instance