Python Django通过manytomanyfield获取查询值
我是django的新手。Python Django通过manytomanyfield获取查询值,python,django,Python,Django,我是django的新手。 我以前练习过如何将数据保存到数据库中。 但现在我不知道如何查询我的数据。 请帮帮我,谢谢 这是我的模型:我在电影加热器上使用了很多文件 class Movie(models.Model): link = models.URLField() title = models.CharField(max_length=255, null=True) title_en = models.CharField(max_length=255, null=True
我以前练习过如何将数据保存到数据库中。
但现在我不知道如何查询我的数据。
请帮帮我,谢谢 这是我的模型:我在电影加热器上使用了很多文件
class Movie(models.Model):
link = models.URLField()
title = models.CharField(max_length=255, null=True)
title_en = 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)
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)
在my views.py中:
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from movie.models import Movie,MovieShowtime
from django.shortcuts import render
def index(request):
movies = Movie.objects.all()
times = MovieShowtime.objects.all()
contacts = get_paginator(request, movies, 10)
return render(request, "movie/index.html",
{'contacts':contacts,'movies':movies,'times':times})
def get_paginator(request, object_list, num_per_page):
paginator = Paginator(object_list, num_per_page)
page = request.GET.get('page')
try:
ret = paginator.page(page)
except PageNotAnInteger:
ret = paginator.page(1)
except EmptyPage:
ret = paginator.page(paginator.num_pages)
return ret
我的电影/index.html:
<div class="activity">
{% for movie in contacts %}
<div class="col-md-12 item">
<p><h3>{{ movie.title }}</h3></p>
{{movie.id}}
<div class="theater">
{% for theater in movie.movietheater_set.all %}
{{ theater.movie_theater }}
{{ theater.id}}
<!--I have to filter {{movie.id}} and {{ theater.id}} to get the time like:
MovieShowtime.objects.filter(movie_id='9',theater_id='5')So I can get the movietime.(I don't know if it is the normal way,It's what I can think now,please guide me)
But I don't know how to do.
-->
{% endfor %}
</div>
</div>
{% endfor %}
</div>
{contacts%%中的电影为%s}
{{movie.title}
{{movie.id}
{moviet.moviet\u set.all%中的剧场为%
{{剧院.电影院}
{{theater.id}
{%endfor%}
{%endfor%}
因为相关的名字,它很混乱。相关名称是oposite实体将访问其定义位置的名称
如果您这样做:
class MovieShowtime(models.Model):
theater = models.ForeignKey(MovieTheater, null=True, related_name='showtimes' )
movie = models.ForeignKey(Movie, null=True, related_name='showtimes' )
然后您将访问电影放映时间,如:
theater = MovieTheater.object.get(movie_theater='test')
for showtime in theater.showtimes.all:
print "Movie: {} - Time: {}".format(showtime.movie.title, showtime.time)
你的例子是什么
get_paginator
-是一种基于类的视图方法。在视图中显示所有导入。py
好的,我编辑它。这是我在Internet上找到的分页器。在你的索引
视图中,你有标题
未定义的变量。当你输入该视图的url时,你在浏览器中看到了什么?对不起,它是标题='嘿'
它只是用于测试index.html是否获得了变量。已经没用了,我现在删除了。
theater = MovieTheater.object.get(movie_theater='test')
for showtime in theater.showtimes.all:
print "Movie: {} - Time: {}".format(showtime.movie.title, showtime.time)