1

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?

japheth
  • 373
  • 1
  • 10
  • 34
  • 1
    What does `{{ property.property_features }}` give? – HenryM Jun 28 '19 at 20:11
  • It gives `listing_admin_data.PropertyFeatures.None` as described above in the question. – japheth Jun 28 '19 at 20:13
  • 1
    What about `{{ property.property_features.all }}`? – Daniel Roseman Jun 28 '19 at 20:30
  • `{{ property.property_features.all }}` gives `` – japheth Jun 28 '19 at 20:34
  • 1
    Sorry, Danielo asked what I meant to type. So that means you've nothing in the queryset which is why you're getting none – HenryM Jun 28 '19 at 20:38
  • Actually, from admin backend, the related property has property features. I really don't know why nothing is loaded. Could the `model's` location in different apps be an issue? – japheth Jun 28 '19 at 20:41
  • Actually, now it is working. It is true the feature list was empty, tried editing from backend and now it is returning the list as it should: `, , , ]>`. Thanks for your insights. – japheth Jun 28 '19 at 20:44

0 Answers0