Django foreignkey查询

时间:2014-10-21 02:42:58

标签: python django

我有这样的模特 Allmovie保存电影名称,
AllmovieTheater保存播放此电影的影院,
AllMovieShowtime节省了剧院的播放时间

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)

例如: 电影'ABC'在剧院超级剧院和男子剧院 我想知道超级剧院的showtime

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

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

但我不知道如何查询它。 我的查询有错误:

>>>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'

请教我谢谢

3 个答案:

答案 0 :(得分:1)

电影可以在很多影院播放,影院可以播放很多电影。因此,“电影”模型和“剧院”模型之间的关系应该是多对多关系,“ShowTime”是用于管理这种关系的中间模型。

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)

要知道电影“ABC”在剧院“超级”中的播出时间:

>>> 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'

答案 1 :(得分:0)

obj2是一个QuerySet对象。

您应该像任何迭代器一样访问obj2的元素。

对于你的情况,获得obj3的可能方法是:

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

答案 2 :(得分:0)

你尝试过类似的事情吗?

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