Reputation: 10878
Please note I am new to django and I am asking this question since I didn't find any answers online on it. I'm also not native speaking english, so apologies if my question is not formulated correctly.
I have two apps, Profile and Submission.
Inside Submission
I have a model called Track (Note the related name in the ForeignKey):
class Track(models.Model):
user = models.ForeignKey(User, unique=False, related_name = 'tracks')
title = models.CharField(max_length=100)
Inside Profile
, I have a model called UserProfile
class UserProfile(models.Model):
user = models.OneToOneField(User)
display_name = models.CharField(max_length=50, default="")
I have a DetailView for UserProfile
inside Profile/views.py
:
class ProfileDetails(DetailView):
model = UserProfile
def get_object(self, queryset=None):
return get_object_or_404(
UserProfile,
user__username=self.kwargs['username'],
)
So inside my template for the DetailView, this should work right? (object
represents the instance of UserProfile
displayed by DetailView)
{% for track in object.tracks.all %}
{{ track }}
{% endfor %}
Instead, I just get nothing. There is no output, why isn't this working?
Upvotes: 0
Views: 35
Reputation: 599638
No; object
here is an instance of UserProfile, which has no direct relationship with Track. You need to go via User, which does.
{% for track in object.user.tracks.all %}
Upvotes: 1