user2492364
user2492364

Reputation: 6713

Django foreignkey query

I have a model like this Allmovie save the name of the movie,
AllmovieTheater save the theaters which playing this movie,
and AllMovieShowtime save the playing time in the theater

class AllMovie(models.Model):
    link = models.URLField()
    title = models.CharField(max_length=100, null=True)
    releaseday = models.CharField(max_length=100, null=True)               
    def __unicode__(self):
        return '%s' % (self.title)

class AllMovieTheater(models.Model):
    allmovietheater  = models.ForeignKey(AllMovie,null=True,blank=True)
    movie_theater = models.CharField(max_length=255, null=True)     
    def __unicode__(self):
        return '%s' % (self.movie_theater)

class AllMovieShowtime(models.Model):
    theater = models.ForeignKey( AllMovieTheater, null=True,blank=True,related_name = 'theater' )
    movie = models.ForeignKey( AllMovie, null=True,blank=True,related_name = 'movie' )
    time = models.CharField(max_length=100, null=True)
    def __unicode__(self):
        return '%s' % (self.time)

for example: movie 'ABC' was in theaters super theater and man theater
I want to know the showtime in super theater

Movie  |      theater          |   showtime
 ABC        super theater           8:00
                                    9:30

Movie  |      theater          |  showtime
 ABC          man theater           8:00 
                                   10:00

But I donn't know how to query it. And my query has error:

>>>obj = AllMovie.objects.get(title='ABC')  
<AllMovie: ABC>
>>>obj2 = allmovietheater_set.filter(movie_theater=’super theater')
[<AllMovieTheater: super theater>
>>>obj3 = obj2.theater_set.all()
AttributeError: 'QuerySet' object has no attribute 'theater_set'

Please teach me Thank you

Upvotes: 0

Views: 55

Answers (3)

neofoo
neofoo

Reputation: 26

A movie can be played in many theaters, and a theater shows many movies. So the relationship between a "Movie" model and a "Theater" model should be many-to-many relationship, and the "ShowTime" is the intermediate model that will be used to govern this relationship.

class Movie(models.Model):
    link = models.URLField()
    title = models.CharField(max_length=100, null=True)
    releaseday = models.CharField(max_length=100, null=True)               
    def __unicode__(self):
        return '%s' % (self.title)

class Theater(models.Model):
    movies = models.ManyToManyField(Movie, null=True, blank=True, through="ShowTime")
    theater = models.CharField(max_length=255, null=True)     
    def __unicode__(self):
        return '%s' % (self.movie_theater)

class ShowTime(models.Model):
    theater = models.ForeignKey(Theater, null=True, blank=True)
    movie = models.ForeignKey(Movie, null=True, blank=True)
    time = models.CharField(max_length=100, null=True)
    def __unicode__(self):
        return '%s' % (self.time)

To know the showtime of the movie "ABC" in the theater "super":

>>> ShowTime.objects.filter(theater__theater="super", movie__title="ABC")
[<ShowTime: ShowTime object>, <ShowTime: ShowTime object>]
>>> ShowTime.objects.filter(theater__theater="super", movie__title="ABC")[0].time
'8:00'
>>> ShowTime.objects.filter(theater__theater="super", movie__title="ABC")[1].time
'10:00'

Upvotes: 1

whale_steward
whale_steward

Reputation: 2268

have you tried something like :

AllMovieShowtime.objects.filter(movie__title="ABC",theater__movie_theater="super theater")

Upvotes: 0

Rikka
Rikka

Reputation: 1049

obj2 is a QuerySet object.

You should access elements of obj2 like any iterator.

For you situation, a possible way to get obj3 is:

obj3 = [x.theater_set.all() for x in obj2]

Upvotes: 0

Related Questions