I have two models related via ManyToMany
relationship, but they are located in separate apps. I am trying to load details of one model then adding the manytomany
field in the template but the conventional way is not working. Here is my code so far:
models.py (listings app)
from listing_admin_data.models import PropertyFeatures
class Property(models.Model):
....
property_features = models.ManyToManyField(PropertyFeatures)
....
models.py (listing_admin_data app)
class PropertyFeatures(models.Model):
....
feature_name = models.CharField(max_length=50)
....
views.py
class PropertyDetailView(DetailView):
model = Property
context_object_name = 'property'
Then in the template, I am trying to do this, but getting empty list, yet I have data.
<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>
I tried this intentionally: <p>{{ property.property_features }}</p>
And upon loading I get: listing_admin_data.PropertyFeatures.None
on the browser.
The other fields directly related to the object loaded are working fine, like {{ property.price }}
, while those from ForeignKey
are also working fine, i.e. {{ property.listing.listing_title }}
. Is inter-apps model relationship handled the same way in Django or it has a special treatment?