如何模板向后过滤两个不同的模型?

时间:2019-08-06 18:22:03

标签: django django-models django-templates django-template-filters

我有三种模型,分别是“属性”,“所有者”和“ 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 个答案:

答案 0 :(得分:0)

您无法访问相关经理的相关经理,您可以在此处使用双{% 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

ContactsOwner.objects.filter(owner__property=property)