如何遍历 django 模板中的相关对象列表?
How do I iterate through a list of related objects in a django template?
我是 Django 的新手,我正在尝试在视图中打印相关模型。我很确定数据库本身设置正确,包括密钥等,并且 table 中有数据可供使用。我不确定的是
- 我是否将模型连接到正确的 table 和列名称?
- 我是否为每位选民加载了 phone?
- 如何迭代关系本身?
我可以自己获取选民信息,但我尝试使用相关的 Phone
模型没有任何效果。
表格:
voters:
id int primary key
first_name varchar
phones:
id int primary_key
phone_number varchar
voter_phones:
voter_id FK
phone_id FK
型号:
from django.db import models
class Phone(models.Model):
phone_number = models.CharField(max_length=255)
class Meta:
db_table = 'phones'
class Voter(models.Model):
first_name = models.CharField(max_length=255)
phones = models.ManyToManyField('Phone', through='VoterPhone', related_name='voters', through_fields=('voter_id', 'phone_id'))
class Meta:
db_table = 'voters'
class VoterPhone(models.Model):
voter = models.ForeignKey('Voter', related_name='voter_phones')
phone = models.ForeignKey('Phone', related_name='voter_phones')
class Meta:
db_table = 'voter_phones'
观点:
def results(request):
voters = Voter.objects.all()
return render(request, 'site/results.html', {'voters': voters})
模板:
{% for voter in voters %}
{{ voter.first_name }}
{% for phone in voter.phones_set.all %}
{{ phone.phone_number }}
{% endfor %}
{% endfor %}
最终结果是选民名单,没有 phone 号码。我错过了什么?
编辑:我创建了一个本地数据库,生成了 运行 django 迁移,并插入了一些虚拟数据。我仍然没有 phone 个号码。
如果您不使用 Django 创建数据库,则需要为 VoterPhone class 指定 table 名称。 Django 不会选择 voterphones table 作为中间 table。我认为通过设置
class 元:
db_table = 'voterphones'
对于 VoterPhone class,您应该能够看到相关的 phone 号码。
我终于得到了一些工作。我必须进行以下更改:
将表重命名为默认的 Django 名称(即 voters_phones 而不是 voter_phones)
发现 voters_phones 中的外键是 int(11) 而 voters 和 phones 中的主键都是 bigint(20)
删除了连接模型
谢谢大家的帮助。
我是 Django 的新手,我正在尝试在视图中打印相关模型。我很确定数据库本身设置正确,包括密钥等,并且 table 中有数据可供使用。我不确定的是
- 我是否将模型连接到正确的 table 和列名称?
- 我是否为每位选民加载了 phone?
- 如何迭代关系本身?
我可以自己获取选民信息,但我尝试使用相关的 Phone
模型没有任何效果。
表格:
voters:
id int primary key
first_name varchar
phones:
id int primary_key
phone_number varchar
voter_phones:
voter_id FK
phone_id FK
型号:
from django.db import models
class Phone(models.Model):
phone_number = models.CharField(max_length=255)
class Meta:
db_table = 'phones'
class Voter(models.Model):
first_name = models.CharField(max_length=255)
phones = models.ManyToManyField('Phone', through='VoterPhone', related_name='voters', through_fields=('voter_id', 'phone_id'))
class Meta:
db_table = 'voters'
class VoterPhone(models.Model):
voter = models.ForeignKey('Voter', related_name='voter_phones')
phone = models.ForeignKey('Phone', related_name='voter_phones')
class Meta:
db_table = 'voter_phones'
观点:
def results(request):
voters = Voter.objects.all()
return render(request, 'site/results.html', {'voters': voters})
模板:
{% for voter in voters %}
{{ voter.first_name }}
{% for phone in voter.phones_set.all %}
{{ phone.phone_number }}
{% endfor %}
{% endfor %}
最终结果是选民名单,没有 phone 号码。我错过了什么?
编辑:我创建了一个本地数据库,生成了 运行 django 迁移,并插入了一些虚拟数据。我仍然没有 phone 个号码。
如果您不使用 Django 创建数据库,则需要为 VoterPhone class 指定 table 名称。 Django 不会选择 voterphones table 作为中间 table。我认为通过设置 class 元: db_table = 'voterphones' 对于 VoterPhone class,您应该能够看到相关的 phone 号码。
我终于得到了一些工作。我必须进行以下更改:
将表重命名为默认的 Django 名称(即 voters_phones 而不是 voter_phones)
发现 voters_phones 中的外键是 int(11) 而 voters 和 phones 中的主键都是 bigint(20)
删除了连接模型
谢谢大家的帮助。