我想设计一个网址:
url(r'^theaters/(?P<area>.+)/(?P<title>.+)/$', TheaterAreaList.as_view(), name='theater-area'),
然后我可以转到以下链接:
http://127.0.0.1:8000/theaters/north/moviename
但是title
和area
在不同的模型中有很多关系:
models.py
class Movie(models.Model):
link = models.URLField()
title = models.CharField(max_length=255, null=True)
class MovieTheater(models.Model):
movietheater = models.ManyToManyField(Movie,null=True,blank=True,through="MovieShowtime")
movie_theater = models.CharField(max_length=255, null=True)
city = models.CharField(max_length=255, null=True) #east west north south
class MovieShowtime(models.Model):
theater = models.ForeignKey( MovieTheater, null=True,blank=True,related_name = 'theater' )
movie = models.ForeignKey( Movie, null=True,blank=True,related_name = 'movie' )
time = models.TextField(null=True,blank=True)
我的views.py有错误QuerySet' object has no attribute 'movietheater_set'
views.py:
class TheaterAreaList(generics.ListAPIView):
serializer_class = TheaterSerializer
def get_queryset(self):
area = self.kwargs['city']
title = self.kwargs['title']
return MovieTheater.objects.filter(city=area).movietheater_set.filter(title=title)
请教我如何查询两个模型,非常感谢。
答案 0 :(得分:0)
_set
仅适用于对象,而不适用于过滤的查询集。
试试这个:
MovieTheater.objects.filter(city=area, movietheater__movie__title=title)