0

我有两个通过ManyToMany关系相关的模型,但它们位于不同的应用程序中。我正在尝试加载一个模型的详细信息,然后manytomany在模板中添加字段,但传统方式不起作用。到目前为止,这是我的代码:

models.py(列表应用程序)

from listing_admin_data.models import PropertyFeatures


class Property(models.Model):
    ....
    property_features = models.ManyToManyField(PropertyFeatures)
    ....

models.py(listing_admin_data 应用程序)

class PropertyFeatures(models.Model):
    ....
    feature_name = models.CharField(max_length=50)
    ....

视图.py

class PropertyDetailView(DetailView):
    model = Property
    context_object_name = 'property'

然后在模板中,我试图这样做,但得到空列表,但我有数据。

<h3>Property Features</h3>
<ul class="amenities-list">
    {% for feature in property.property_features.all %}
        <li>{{ feature.feature_name }}</li>
        {% empty %}
        <li>Property has no features!</li>
    {% endfor %}
</ul>

我故意尝试了这个:<p>{{ property.property_features }}</p> 加载后我得到:listing_admin_data.PropertyFeatures.None在浏览器上。与加载的对象直接相关的其他字段工作正常,例如{{ property.price }},而那些 fromForeignKey也工作正常,即{{ property.listing.listing_title }}. 应用程序间模型关系在 Django 中是否以相同的方式处理,还是有特殊处理?

4

0 回答 0