【发布时间】:2014-04-14 07:21:00
【问题描述】:
在 Django 版本的 SQL 多表查询方面需要一些帮助。该查询使用 3 个表从Restaurants table 检索餐厅名称、地址和从Cuisinetypes table 检索美食类型。全部基于通过 URL 传递的菜品名称,菜品 ID 存储在菜品表中。
Models.py
class Restaurant(models.Model):
name = models.CharField(max_length=50, db_column='name', blank=True)
slugname = models.SlugField(max_length=50, blank=True)
address = models.CharField(max_length=100, blank=True)
city = models.ForeignKey('City', related_name="restaurants")
location = models.ForeignKey('Location', related_name="restaurants")
hood = models.ForeignKey('Hood', null=True, blank=True, related_name="restaurants")
listingrole = models.ForeignKey('Listingrole', related_name="restaurants")
cuisine_types = models.ManyToManyField('Cuisinetype', null=True, blank=True, related_name="restaurants")
class Meta:
db_table = 'restaurant'
class City(models.Model):
name = models.CharField(max_length=50, db_column='city')
state = models.CharField(max_length=50, blank=True, null=True)
switch = models.SmallIntegerField(null=True, blank=True, default='1')
class Meta:
db_table = 'city'
class Cuisinetype(models.Model):
name = models.CharField(max_length=50, db_column='cuisine', blank=True) # Field name made lowercase.
switch = models.SmallIntegerField(null=True, blank=True, default='1')
class Meta:
db_table = 'cuisinetype'
class Location(models.Model):
name = models.CharField(max_length=50, db_column='location', blank=False, null=False)
city = models.ForeignKey('City', related_name="locations")
switch = models.SmallIntegerField(null=True, blank=True, default='1')
class Meta:
db_table = 'location'
class Hood(models.Model):
name = models.CharField(max_length=50, db_column='hood')
city = models.ForeignKey('City', related_name='hoods')
location = models.ForeignKey('Location', related_name='hoods')
switch = models.SmallIntegerField(null=True, blank=True, default='1')
class Meta:
db_table = 'hood'
class Listingrole(models.Model):
id = models.AutoField(primary_key=True, db_column='id')
name = models.CharField(max_length=50, db_column='listingrole', blank=True) # Field name made lowercase.
switch = models.SmallIntegerField(null=True, blank=True, default='1')
class Meta:
db_table = 'listingrole'
urls.py
url(r'^cuisine/(?P<cuisine>[-\w]+)/$', 'views.cuisinesearch'),
views.py
def cuisinesearch(request, name='unknown'):
name = name.replace('-', ' ').capitalize()
return render_to_response('cuisinesearch.html',
{'cuisinesearch': Restaurant.objects.filter(city_id=8, switch=1, listingrole__in=[1,2,3,4], cuisine_types__name=name)
.distinct().prefetch_related("cuisine_types").order_by('listingrole', 'displayorder')[:50] })
HTML
还有什么是显示查询的正确方法?
{% for restaurant in cuisinesearch %}
<h2>{{ restaurant.name }}</h2>
<div class="location">{{ restaurant.location }}</div>
<h3>Cuisines:</h3>
<ul class="cuisines">{% for ct in restaurant.cuisine_types.all %}
<li>{{ ct.name }}</li>{% endfor %}
</ul>
{% endfor %}
【问题讨论】:
-
使用 Django 的大部分意义在于使用它的 ORM 为你做这种事情,即使用 models,而不是执行任意 SQL。请出示您的型号代码。
-
根据您的要求,我添加了模型。谢谢!
-
您似乎试图强迫 Django 以某种方式制作数据库,而不是让它做它擅长的事情。您为什么不看一下 Django 文档,了解它如何处理 many-to-many relationships 并相应地调整您的模型...
-
我只是在处理我已经从另一个项目中获得的东西,而且几乎不可能从头开始,因为我已经有超过 100 个包含数据的表。
-
你看过this吗?
标签: python html mysql sql django