如何模板过滤两个不同的模型?
Posted
技术标签:
【中文标题】如何模板过滤两个不同的模型?【英文标题】:How can I template filter backwards two different models? 【发布时间】:2019-12-14 08:30:51 【问题描述】:我有三个模型,“Property”、“Owners”和“ContactsOwner”,最后一个是我保存所有者联系人的地方。
# APP: Owners - models.py
class Owner(models.Model):
name = models.CharField(db_column='Owner_Name', max_length=200)
surname = models.CharField(db_column='Owner_Surname', max_length=30)
nif = models.IntegerField(db_column='Nif_Number', blank=False, null=False, default='000000000')
doc_number = models.CharField(db_column='Document_Number', max_length=20, blank=True, null=True)
doc_type = models.CharField(db_column='Document_Type', max_length=20, blank=True, null=True, choices=DOCUMENT_TYPE_CHOICES)
address = models.CharField(db_column='Address', max_length=200, blank=True, null=True)
post_code = models.CharField(db_column='Post_Code', max_length=15, blank=True, null=True)
nationality = models.CharField(db_column='Country', max_length=20, blank=True, null=True)
notes = models.CharField(db_column='Notes', max_length=250, blank=True, null=True)
property = models.ManyToManyField(Property)
class Meta:
db_table = 'Owner'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("owners:owner_detail",kwargs='pk':self.pk)
class ContactsOwner(models.Model):
owner = models.ForeignKey(Owner, models.DO_NOTHING, db_column='Owner', related_name='owner_contact') # Field name made lowercase.
type = models.CharField(db_column='Type', choices=CONTACT_TYPE_CHOICES, max_length=25, blank=True, null=True) # Field name made lowercase.
number = models.IntegerField(db_column='Number', blank=True, null=True) # Field name made lowercase.
email = models.CharField(db_column='Email', max_length=100, blank=True, null=True) # Field name made lowercase.
class Meta:
db_table = 'Contacts_Owner'
verbose_name_plural = "Owners Contacts"
# APP: Properties - models.py
class Property(models.Model):
property_reference = models.CharField(db_column='Property_Reference', max_length=10) # Field name made lowercase.
address = models.CharField(db_column='Address', max_length=250, blank=True, null=True) # Field name made lowercase.
post_code = models.CharField(db_column='Post_Code', max_length=15, blank=True, null=True) # Field name made lowercase.
type = models.CharField(db_column='Type', max_length=25, blank=True, null=True, choices=HOUSE_TYPE_CHOICES) # Field name made lowercase.
bedrooms = models.IntegerField(db_column='Bedrooms', blank=True, null=True) # Field name made lowercase.
bathrooms = models.IntegerField(db_column='Bathrooms', blank=True, null=True) # Field name made lowercase.
usual_cleaning_requirements = models.CharField(db_column='Usual_Cleaning_Requirements', max_length=250, blank=True, null=True) # Field name made lowercase.
notes = models.CharField(db_column='Notes', max_length=500, blank=True, null=True) # Field name made lowercase.
feature_image = models.ImageField(null=True)
class Meta:
db_table = 'Property'
def __str__(self):
return self.property_reference
def get_absolute_url(self):
return reverse("properties:property_detail",kwargs='pk':self.pk)
在我的模板中,我可以通过此循环访问所有者姓名和姓氏:
% for object in property.owner_set.all %
<h4> object.name object.surname </h4>
<h6>Owner</h6>
% endfor %
但现在我需要访问“ContactsOwner”模型上的联系电话,但我无法找到过滤此信息的方法。
我试过这些:
% for contact in property.owner_set.owner_contact.all %
<div class="card-body border-top">
contact.number
<br>
</div>
% endfor %
但到目前为止我还没有找到解决方案。你能帮我解决这个问题吗?
【问题讨论】:
【参考方案1】:您不能访问相关经理的相关经理,您可以在此处使用双 % for ... %
循环,但这是不可取的:
% for owner in property.owner_set.all %
% for contact in owner.owner_contact.all %
<div class="card-body border-top">
contact.number
<br>
</div>
% endfor %
% endfor %
最好在视图中执行查询,因为这主要是业务逻辑。您可以通过以下方式检索给定属性的所有 OwnerContact
s:
ContactsOwner.objects.filter(<b>owner__property=<i>property</i></b>)
【讨论】:
感谢您的帮助!!我已经创建了这个class PropertyDetailView(DetailView): template_name = 'properties/property-detail.html' model = Property def get_context_data(self, **kwargs): contacts = ContactsOwner.objects.filter(owner__property=property) context = super().get_context_data(**kwargs) context['contact'] = contacts return context
它给了我一个 TypeError: int()... 我找到了一篇帖子,你已经回答了同样的问题,我试过这样做,但它仍然给了我错误。谢谢!
@LuisSilva:你可能应该在这里使用context['property']
,所以.filter(owner__property=context['property'])
。
我写的“self.object”很糟糕。现在它工作得很好。我做了这些“.filter(owner__property=self.object)”。太感谢了!祝你一切顺利!以上是关于如何模板过滤两个不同的模型?的主要内容,如果未能解决你的问题,请参考以下文章
Django 在一个模板中使用两个 FilterView 类
是否可以使用基于类的通用 ListView 在单个模板中同时过滤和排序?如果是,如何?