Django combine foreign keys in a single queryset - python

I have a model in a Django application that is being referenced by multiple other models as a ForeignKey.
What I am looking for is for a way to create a single queryset for all objects of this class that are being referenced as ForeignKey by the rest of the classes based on some criteria.
I am not even sure if this is possible, but I thought about asking anyway.
class Person(models.Model):
pass
class Book(models.Model):
year_published = models.PositiveIntegerField()
author = models.ForeignKey(Person)
class MusicAlbum(models.Model):
year_published = models.PositiveIntegerField()
producer = models.ForeignKey(Person)
recent_books = Book.objects.filter(year_published__gte=2018)
recent_music_albums = MusicAlbum.objects.filter(year_published__gte=2018)
# How can I create a **single** queryset of the Person objects that are being referenced as `author` in `recent_books` and as `producer` in `recent_music_albums`?
Thanks for your time.

I don't have Django in front of me at the moment, but what about something like:
class Person(models.Model):
pass
class Book(models.Model):
year_published = models.PositiveIntegerField()
author = models.ForeignKey(Person, related_name='books')
class MusicAlbum(models.Model):
year_published = models.PositiveIntegerField()
producer = models.ForeignKey(Person, related_name='albums')
Person.objects.filter(books__year_published__gte=2018, albums__year_published__gte=2018)
Or, if you have to do those first two queries anyway,
Person.objects.filter(books__in=recent_books, albums__in=recent_music_albums)

You will have on Person model instances a RelatedManager for Books and MusicAlbums. Probably they will still have the default names book_set and musicalbum_set since you didn't override them.
You can use these to find the books/music albums associated with one person instance:
persons_books = person.book_set.all()
persons_musicalbums = person.musicalbum_set.all()
And similarly you can generate the relevant queryset from the model manager:
qs = Person.objects.exclude(book=None).exclude(musicalbum=None)

Same can be achieved by this :
person = Person.objects.latest('book__year_published', 'musicalbum__year_published')
or
personList = Person.objects.all().order_by('-book__year_published', '-musicalbum__year_published')

Related

one-to-one relationship but of multiple types in Django

I'm creating an online shop with Django. I figured since there could be different types of item for sale that share some attributes and fields, I'd better make an Item Model and other models subclass it.
So I now have an abstract Item model and some other models like Dress, Pants and shoes.
Now I wanna have a new model (e.g. Comment) which should have a relationship with the Item model.
But since Item model is abstract I can't do it.
Is there way I could have a one to one relationship whose one side could accept different types?
Some thing like this:
class Comment(models.Model):
item = models.ForeignKey(to=[Dress, Pants, Shoes])
One Foreing key field can lead only to one instance, in a database it would look like this:
|id| item |
|13|t-shirt|
The best way to solve your problem is to use these three models:
class Item_type(models.Model):
#here you should create as many instances as you have types of your items
# one item_type = Dress, second = Pants, third = Shoes
title = models.CharField(max_length=50)
class Item(models.Model):
#here you create your item, with title for example Nike Brand new shooes
title = models.CharField(max_length=150)
#and choosing type in oneToOneField = shooes
item_type = models.OneToOneField(Item_type, on_delete=models.CASCADE)
class Comment(models.Model):
#here you have your comments for your Item
item = models.ForeignKey(Item, on_delete=models.CASCADE)
Generic Relations sounds like solution, in your Comment model add these fields:
class Comment(models.Model):
[Other fields]
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey()
and in your abstract Item model add GenericRelation field
class Item(models.Model):
[Other fields]
comments = GenericRelation(Comment)
class Meta:
abstract=True

Django - manager on ManyToMany relationship

I have 3 models
class Person(models.Model):
name = models.CharField(max_length=128)
class Company(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField (Person, through = 'Membership', related_name = 'companies')
class Membership(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
is_admin = models.BooleanField()
I can then call person.companies.all() to get the list of companies associated with person.
How do I create a manager to have the list of companies associated with person, but whose person is admin (is_admin = True)?
You can create a manager like the following:
managers.py:
from django.db import models
class AdminCompaniesManager(models.Manager):
def get_queryset(self):
return super().get_queryset().companies.filter(membership__is_admin=True)
and then in your Person model (please remind the objects manager):
class Person(models.Model):
name = models.CharField(max_length=128)
objects = models.Manager()
administered_companies = AdminCompaniesManager()
Now you can easily call the following (e.g. in your views):
my_person.administered_companies.all()
PS: a very efficient option (e.g. if you are in a view and you need the list of company ids by a given person) is to query the membership model directly, so you can optimize the query for data retrieval from DB avoiding the joins:
Membership.objects.filter(is_admin=True, person=person).values_list('company_id')
You can filter with:
person.companies.filter(membership__is_admin=True)
This will filter the junction table Membership, such that it will only retrieve Companys for which the Membership has is_admin set to True.
Another option is to retrieve this with:
Company.objects.filter(membership__is_admin=True, members=person)
You can attach this to the Person model with:
class Person(models.Model):
name = models.CharField(max_length=128)
#property
def admin_companies(self):
return self.companies.filter(membership__is_admin=True)

Django get rid of duplicated queries in nested models

I have models as shown below,
class Manufacturer(models.Model):
name = models.CharField(max_length=100)
class Car(models.Model):
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
name = models.CharField(max_length=300)
#property
def latest_variant(self):
return self.carvariant_set.last()
class CarVariant(models.Model):
car = models.ForeignKey(Car, on_delete=models.CASCADE)
name = models.CharField(max_length=300)
and I am making a query to get the latest variant of all cars, I am getting much duplicated queries.
I couldn't eliminate it with prefetch_related
Car.objects.all().prefetch_related('carvariant_set')
How can I eliminate the duplicated queries?
If you use .prefetch_related it will populate the carvariant_set value, but only for a .all() query, not for a .last(), that will trigger a new query.
What we can do is define a property like:
class Car(models.Model):
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
name = models.CharField(max_length=300)
#property
def latest_variant(self):
items = getattr(self, '_latest_variants', ())
if items:
return items[-1]
return self.carvariant_set.last()
Then we can prefetch the related object with:
from django.db.models import Prefetch
Car.objects.prefetch_related(
Prefetch(
'carvariant_set',
queryset=CarVariant.objects.order_by('pk'),
to_attr='_latest_variants'
)
)
To get rid of duplicates you use "distinct()".
For example Car.objects.all().prefetch_related('carvariant_set').distinct(). You can read about it here:
https://docs.djangoproject.com/en/3.2/ref/models/querysets/#django.db.models.query.QuerySet.distinct
sometimes you might need to tell the "distinct" function which fields make an object distinct. By default it's the id, but you can do something like "distinct('name')" in order to avoid getting 2 instances with the same name for example.

How to select same object twice in Django ManyToMany Field

I have two models
class Food(models.Model):
name = models.CharField(max_length=200 ,null=False)
class Profile(models.Model):
food_selected_today = models.ManyToManyField(Food,related_name = 'inventory')
Now in profile model I want to have one food with same id for example Apple more than one time in food_selected_today. If I now add same food twice it only shows one Item. How can I add one food many times here.
Any kind of help would be really appreciated
Generally this is not possible natively with the built in relationship fields, but you can use your own through-model to give you the opportunity to have a count attribute for each relation:
class Food(models.Model):
name = models.CharField(max_length=200, null=False)
class Profile(models.Model):
food_selected_today = models.ManyToManyField(Food,
related_name='inventory',
through='ProfileFood')
class ProfileFood(models.Model):
food = models.ForeignKey(Food)
profile = models.ForeignKey(Profile)
count = models.IntegerField()

Django Inheriting from classes

I have run into a problem developing my Django site.
from django.db import models
class TitlePost(models.Model):
title_name = models.CharField(max_length=100, unique=True)
title_body = models.TextField(max_length=30000)
title_why = models.TextField(max_length=250, null=True)
title_publication_date = models.DateTimeField('date')
likes = models.IntegerField(default=0)
dislikes = models.IntegerField(default=0)
def __unicode__(self):
return self.title_name
class TopTitlesPostPage(models.Model):
title_post = models.OneToOneField(TitlePost)
hello = models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.hello
class NewTitlesPostPage(models.Model):
title_post = models.OneToOneField(TitlePost)
hello = models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.hello
Why don't TopTitlesPostPage and NewTitlesPostPage inherit all the attributes from TitlePost? For instance, if I try to call the likes in my template using TopTitlesPostPage, it will not execute because the likes attribute is not inherited. Does OneToOneField have something to do with the problem? I did read that making TitlePost a meta class will help but I need it to have a table in my database. I actually want all of them to have a table in my data base. Then again, maybe I am approaching this the wrong way and I should use just TitlePost as a model to generate everything?
The behaviour you would like to see is called multi table inheritance. Every child class internally ends up with the same thing that you wrote, so with a one to one field to the base class TitlePost, but it's internally managed by django.
If you do multiple inheritance like the code below you will be able to write:
k=TopTitlesPostPage.objects.create(hello="Hello",title_name="Heh")
That means the fields will be directly accessible.
from django.db import models
class TitlePost(models.Model):
title_name = models.CharField(max_length=100, unique=True)
title_body = models.TextField(max_length=30000)
title_why = models.TextField(max_length=250, null=True)
title_publication_date = models.DateTimeField('date')
likes = models.IntegerField(default=0)
dislikes = models.IntegerField(default=0)
def __unicode__(self):
return self.title_name
class TopTitlesPostPage(TitlePost):
hello = models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.hello
class NewTitlesPostPage(TitlePost):
hello = models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.hello
In case you are never actually going to reference the base class TitlePost, but only its children it might be more appropriate to make `TitlePost abstract:
class TitlePost(models.Model):
title_name = models.CharField(max_length=100, unique=True)
title_body = models.TextField(max_length=30000)
title_why = models.TextField(max_length=250, null=True)
title_publication_date = models.DateTimeField('date')
likes = models.IntegerField(default=0)
dislikes = models.IntegerField(default=0)
class Meta:
abstract = True
def __unicode__(self):
return self.title_name
Making TitlePostabstract will omit the creation of the table TitlePostin the database, and the child models will end up with the fields of the base class inserted into their own tables seperately. If the base class is just for factoring out common functionality this is the preferred way to go.
For huge queries this will also make a difference in performance because the ORM will need to do less JOINoperations.
It's not possible to install Foreign Keys to abstract models in Django.
You can however install Foreign Keys to a non abstract base class. The only limitation is that the reverse Foreign Key relation will return the base class instances.
You can circumvent this limitation by using django-polymorphic.
Django Polymorphic allows you to query the base class objects but retrieves the child class instances:
>>> Project.objects.create(topic="Department Party")
>>> ArtProject.objects.create(topic="Painting with Tim", artist="T. Turner")
>>> ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
>>> Project.objects.all()
[ <Project: id 1, topic "Department Party">,
<ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">,
<ResearchProject: id 3, topic "Swallow Aerodynamics", supervisor "Dr. Winter"> ]
To use django polymorphic you only need to declare your models with Polymorphic Model as base class:
from django.db import models
from polymorphic import PolymorphicModel
class ModelA(PolymorphicModel):
field1 = models.CharField(max_length=10)
class ModelB(ModelA):
field2 = models.CharField(max_length=10)
class ModelC(ModelB):
field3 = models.CharField(max_length=10)
Foreign keys will also return the child class instances, which is really cool if you're trying to be polymorphic.
# The model holding the relation may be any kind of model, polymorphic or not
class RelatingModel(models.Model):
many2many = models.ManyToManyField('ModelA') # ManyToMany relation to a polymorphic model
>>> o=RelatingModel.objects.create()
>>> o.many2many.add(ModelA.objects.get(id=1))
>>> o.many2many.add(ModelB.objects.get(id=2))
>>> o.many2many.add(ModelC.objects.get(id=3))
>>> o.many2many.all()
[ <ModelA: id 1, field1 (CharField)>,
<ModelB: id 2, field1 (CharField), field2 (CharField)>,
<ModelC: id 3, field1 (CharField), field2 (CharField), field3 (CharField)> ]
Take into account that these queries will be slightly less performant.
U need to extend the classes like follows:
class TopTitlesPostPage(TitlePost):
U can add more and inherit from multiple models just by mentionin g all the models comma separated! This all the fields from the models will be created in the child class as well
EDIT:
The way i would do it is to create an Abstract class which contains all your common fields and extend it into your TitlePost, TopTitlesPostPagea and NewTitlesPostPage
You need to have TopTitlesPostPage and NewTitlesPostPage extend the base class of TitlePost like so ...
class TopTitlesPostPage(models.Model)
You don't need a OneToOneField if you are inheriting from the base class, since the attributes of TitlePost will be available to you in the subclass. If you want to make TitlePost abstract (you can not declare an instance of that class, only inherit from it) you have to add it to the meta class
class TitlePost(models.Model):
class Meta:
abstract = True
Here is a link to the documentation.

Categories

Resources