Django Models and SELECT with joined two or more tables? - django

How can I make an select with two (or more) joined tables using Django Models?
For example:
class Album(models.Model):
artist = models.ForeignKey(Musician)
name = models.CharField(max_length=100)
release_date = models.DateField()
num_stars = models.IntegerField()
class Song(models.Model):
album = models.ForeignKey(Album)
name = models.CharField(max_length=100)
num_stars = models.IntegerField()
SELECT * from album, song where (album.id = song.album_id) and (album.artist_id = X)

Django querysets want to return lists of model instances, which doesn't quite map to the the SQL notion of returning data from multiple tables joined into a single result table.
To achieve the effect of the SQL query you provide as an example, you could do one of the following:
1) If you want to iterate over your data by Songs, you can limit the songs you query by the artist id, like so:
songs = Song.objects.filter(album__artist__id=123)
for song in songs:
print song.name
print song.album.name
... do what ever you need to do with song here ...
If you are concerned about performance, you can add select_related() to your queryset.
# the depth argument tells django to only follow foreign key relationships
# from Song one level deep
songs = Song.objects.select_related(depth=1).filter(album__artist__id=123)
for song in songs:
# django has now already fetched the album data, so accessing it
# from the song instance does not cause an additional database hit
print song.album.name
2) If you want to iterate over your data by Album, you can do things like so:
albums = Album.objects.filter(artist__id = 123)
for album in albums:
for song in album.song_set.all():
print song.name
print album.name
... etc ...

You can perform raw sql query, but that's probably not the thing you want to do. Explain how your Models look like and what you want to achieve and probably django ORM will be enough to perform the query.

Related

how to select fields in related tables quickly in django models

I'm trying to get all values in current table, and also get some fields in related tables.
class school(models.Model):
school_name = models.CharField(max_length=256)
school_type = models.CharField(max_length=128)
school_address = models.CharField(max_length=256)
class hometown(models.Model):
hometown_name = models.CharField(max_length=32)
class person(models.Model):
person_name = models.CharField(max_length=128)
person_id = models.CharField(max_length=128)
person_school = models.ForeignKey(school, on_delete=models.CASCADE)
person_ht = models.ForeignKey(hometown, on_delete=models.CASCADE)
how to quick select all info i needed into a dict for rendering.
there will be many records in person, i got school_id input, and want to get all person in this school, and also want these person's hometown_name shown.
i tried like this, can get the info i wanted. And any other quick way to do it?
m=person.objects.filter(person_school_id=1)
.values('id', 'person_name', 'person_id',
school_name=F('person_school__school_name'),
school_address=F('person_school__school_address'),
hometown_name=F('person_ht__hometown_name'))
person_name, person_id, school_name, school_address, hometown_name
if the person have many fields, it will be a hard work for list all values.
what i mean, is there any queryset can join related tables' fields together, which no need to list fields in values.
Maybe like this:
m=person.objects.filter(person_school_id=1).XXXX.values()
it can show all values in school, and all values in hometown together with person's values in m, and i can
for x in m:
print(x.school_name, x.hometown_name, x.person_name)
You add a prefetch_related query on top of your queryset.
prefetch_data = Prefetch('person_set, hometown_set, school_set', queryset=m)
Where prefetch_data will prepare your DB to fetch related tables and m is your original filtered query (so add this below your Person.objects.filter(... )
Then you do the actual query to the DB:
query = query.prefetch_related(prefetch_data)
Where query will be the actual resulting query with a list of Person objects (so add that line below the prefetch_data one).
Example:
m=person.objects.filter(person_school_id=1)
.values('id', 'person_name', 'person_id',
school_name=F('person_school__school_name'),
school_address=F('person_school__school_address'),
hometown_name=F('person_ht__hometown_name'))
prefetch_data = Prefetch('person_set, hometown_set, school_set', queryset=m)
query = query.prefetch_related(prefetch_data)
In that example I've broken down the queries into more manageable pieces, but you can do the whole thing in one big line too (less manageable to read though):
m=person.objects.filter(person_school_id=1)
.values('id', 'person_name', 'person_id',
school_name=F('person_school__school_name'),
school_address=F('person_school__school_address'),
hometown_name=F('person_ht__hometown_name')).prefetch_related('person, hometown, school')

Django make a double foreign key lookup involving one to many to one relations

I have the following models:
def Order(models.Model):
receiver = models.ForeignKey(Receiver)
warehouse = models.ForeignKey(Warehouse)
def Receiver(models.Model):
user = models.ForeignKey(User) #this is not made one to one because user can have more than one receiver
name = ...
zipcode = ...
def Warehouse(models.Model):
city = ...
street = ...
I want to select all Warehouse entries related to request.User object. The only way i can do this now is:
orders = Order.objects.filter(receiver__user=request.User)
# here i set orders warehouse ids to list called ids
user_warehouses = Warehouse.objects.filter(pk__in=ids)
But i have a strong feeling that i am inventing the wheel. Is there a more simple Django-way of doing this?
Warehouse.objects.filter(order__receiver__user=request.user)
you can traverse relations backwards ("reverse lookup") and traverse multiple levels with the double-underscore syntax
https://docs.djangoproject.com/en/1.8/topics/db/queries/#lookups-that-span-relationships

Django-MongoDB recursive call

I'm a beginner in MongoDB and I'm trying to solve one problem I had with a relational database (using Django). I have a Song and Album models, and I want each song to have a reference to its Album, and each Album to have a list of all its songs. Is it possible to have something like this in MongoDB :
{
'song_title': 'In mist she was standing',
'album': {
'album_title': 'Orchid',
'songs': [ { song_title: 'Requiem', album:{'album_title':'Orchid', 'songs':[{song_title'....
And that's when we have an endless loop. Is there anyway to achieve this? Maybe I'm not fully understanding the point of MongoDB, I'd be happy if someone could point me to the right direction.
Thanks!
My suggestion is to keep it simple at start: two collections Album and Song just like in any other relational database.
I'm assuing you're using mongoengine as ORM.
class Album(Document):
album_title = StringField()
songs = ListField(ObjectIdFIeld())
meta = {
'indexes': ['songs'],
'allow_inheritance': False,
}
class Song(Document):
song_title = StringField()
album = ObjectIdField()
meta = {
'indexes': ['album']
}
every time you fetch an Album you can fetch its songs with a second query (mongodb has no concept of joins):
album = Album.objects.all().first() # just an example
songs = Song.objects.filter(id__in=album.songs)
Another possibility is to completely embed song into album:
class Song(EmbeddedDocument):
song_title = StringField()
class Album(Document):
album_title = StringField()
songs = ListField(EmbeddedDocumentField(Song))
Could be better if your queries are (almost) all coming from the Album model.
Anyways I suggest starting with the two table setup and then later on you can improve preformance by embedding song data into album.
Hope this helps

Django using the values method with m2m relationships / filtering m2m tables using django

class Book(models.Model):
name = models.CharField(max_length=127, blank=False)
class Author(models.Model):
name = models.CharField(max_length=127, blank=False)
books = models.ManyToMany(Books)
I am trying to filter the authors so I can return a result set of authors like:
[{id: 1, name: 'Grisham', books : [{name: 'The Client'},{name: 'The Street Lawyer}], ..]
Before I had the m2m relationship on author I was able to query for any number of author records and get all of the values I needed using the values method with only one db query.
But it looks like
Author.objects.all().values('name', 'books')
would return something like:
[{id: 1, name: 'Grisham', books :{name: 'The Client'}},{id: 1, name: 'Grisham', books :{name: 'The Street Lawyer'}}]
Looking at the docs it doesn't look like that is possible with the values method.
https://docs.djangoproject.com/en/dev/ref/models/querysets/
Warning Because ManyToManyField attributes and reverse relations can
have multiple related rows, including these can have a multiplier
effect on the size of your result set. This will be especially
pronounced if you include multiple such fields in your values() query,
in which case all possible combinations will be returned.
I want to try to get a result set of n size with with the least amount of database hits authorObject.books.all() would result in at least n db hits.
Is there a way to do this in django?
I think one way of doing this with the least amount of database hits would be to :
authors = Authors.objects.all().values('id')
q = Q()
for id in authors:
q = q | Q(author__id = id)
#m2m author book table.. from my understanding it is
#not accessible in the django QuerySet
author_author_books.filter(q) #grab all of the book ids and author ids with one query
Is there a built in way to query the m2m author_author_books table or am I going to have the write the sql? Is there a way to take advantage of the Q() for doing OR logic in raw sql?
Thanks in advance.
I think you want prefetch_related. Something like this:
authors = Author.objects.prefetch_related('books').all()
More on this here.
If you want to query your author_author_books table, I think you need to specify a "through" table:
class BookAuthor(models.Model):
book = models.ForeignKey(Book)
author = models.ForeignKey(Author)
class Author(models.Model):
name = models.CharField(max_length=127, blank=False)
books = models.ManyToMany(Books, through=BookAuthor)
and then you can query BookAuthor like any other model.

How to ask the Django manytomany manager to match several relations at once?

I've got this model:
class Movie(models.Model):
# I use taggit for tag management
tags = taggit.managers.TaggableManager()
class Person(models.Model):
# manytomany with a intermediary model
movies = models.ManyToManyField(Movie, through='Activity')
class Activity(models.Model):
movie = models.ForeignKey(Movie)
person = models.ForeignKey(Person)
name = models.CharField(max_length=30, default='actor')
And I'd like to match a movie that has the same actors as another one. Not one actor in common, but all the actors in common.
So I don't want this:
# actors is a shortcut property
one_actor_in_common = Movie.object.filter(activities__name='actor',
team_members__in=self.movie.actors)
I want something that would make "Matrix I" match "Matrix II" because they share 'Keanu Reeves' and 'Laurence Fishburne', but not match "Speed" because they share 'Keanu Reeves' but not 'Laurence Fishburne'.
The many to manytomany manager cannot match several relations at once. Down at the database level it all comes down to selecting and grouping.
So naturally speaking the only question the database is capable to answer is this: List acting activites that involve these persons, group them by movie and show only those movies having the same number of those acting activities as persons.
Translated to ORM speak it looks like this:
actors = Person.objects.filter(name__in=('Keanu Reaves', 'Laurence Fishburne'))
qs = Movie.objects.filter(activity__name='actor',
activity__person__in=actors)
qs = qs.annotate(common_actors=Count('activity'))
all_actors_in_common = qs.filter(common_actors=actors.count())
The query this produces is actually not bad:
SELECT "cmdb_movie"."id", "cmdb_movie"."title", COUNT("cmdb_activity"."id") AS "common_actors"
FROM "cmdb_movie"
LEFT OUTER JOIN "cmdb_activity" ON ("cmdb_movie"."id" = "cmdb_activity"."movie_id")
WHERE ("cmdb_activity"."person_id" IN (SELECT U0."id" FROM "cmdb_person" U0 WHERE U0."name" IN ('Keanu Reaves', 'Laurence Fishburne'))
AND "cmdb_activity"."name" = 'actor' )
GROUP BY "cmdb_movie"."id", "cmdb_movie"."title", "cmdb_movie"."id", "cmdb_movie"."title"
HAVING COUNT("cmdb_activity"."id") = 2
I also have a small application that I used to test this, but I don't know wether someone needs it nor where to host it.