Hi guys, I've been scratching my head on this one. I want to know if it's
possible to filter the data of models class with related_name in a foor
loop? Basically, I want to show the members info associated to a Team.
models.py
class Team_Region(models.Model):
name = models.CharField(max_length=50)
# String representation
def __str__(self):
return self.name
class Team_Name(models.Model):
team_name = models.CharField(max_length=150)
logo = models.ImageField(upload_to='team_logos', blank=True)
region_member = models.ForeignKey(Team_Region,
related_name='region_mem')
def __str__(self):
return self.team_name + ' - ' + str(self.region_member)
class Team_Member(models.Model):
member_name = models.CharField(max_length=150)
position = models.CharField(max_length=50)
member_of_team = models.ForeignKey(Team_Name, related_name='team_mem')
def __str__(self):
return self.member_name + ' - ' + str(self.member_of_team)
views.py
# Listview of Regions
class TeamRegionListView(ListView):
context_object_name = 'region_name'
model = Team_Region
template_name = 'dota_teams/team_region_list.html'
# DetailView of Regions
class TeamRegionDetailView(DetailView):
context_object_name = 'team_names'
model = Team_Region
template_name = 'dota_teams/team_region_detail.html'
# DetailView of Teams. Will show team members
class TeamDetailView(DetailView):
context_object_name = 'team_members'
model = Team_Region
template_name = 'dota_teams/team_detail.html'
urls.py
url(r'^$', views.TeamRegionListView.as_view(), name='region_list'),
url(r'^(?P<pk>\d+)/$', views.TeamRegionDetailView.as_view(),
name='region_detail'),
url(r'^(?P<pk>\d+)/(\d+)/$', views.TeamDetailView.as_view(),
name='team_detail'),
Problem is, in my team_detail.html, for me to access the members' info, I
have these loops:
<div class="row">
{% for team in team_members.region_mem.all %}
{% for member in team.team_mem.all %}
<div class="col-xs-12 col-lg-3">
<div class="thumbnail">
<h4>{{ member.member_name }}</h4>
<div class="caption">
<h5>{{ member.position }}</h5>
</div>
</div>
</div>
{% endfor %}
{% endfor %}
</div>
All members info will appear if i click a team which is not the way I want
it. I only want to show the members info associated to a team that I
clicked. This happens because I hae the team.team_mem.all in my loop which
basically shows all members data including those who are not part of the
team. Is there a way to filter this? My apologies for the long email.
TIA,
jarvis
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAA6wQLKCC1duXTRdru67Ej0MyuvMKL17DM4QTh%2BBmxxsw%2BxeFQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.