Home > Net >  how to utilize from get_queryset with id parameter foreach particular foreign key items in class bas
how to utilize from get_queryset with id parameter foreach particular foreign key items in class bas

Time:09-26

first of all, I am very new to django and thank you for patience, I want to make web site for showing how the league stands and shows the particular team's footballer here what I have done so far

models.py

class League(models.Model):
    name = models.CharField(max_length=255)
    country = models.CharField(max_length=255)
    founded = models.DateField()

    def __str__(self):
        return self.name


class Team(models.Model):
    league = models.ForeignKey(
        League, blank=True, null=True, on_delete=models.CASCADE)
    image = models.ImageField(
        upload_to="images/", default='images/Wolves.png',  blank=True, null=True)
    name = models.CharField(max_length=255)
    win = models.IntegerField(blank=True, null=True)
    draw = models.IntegerField(blank=True, null=True)
    loss = models.IntegerField(blank=True, null=True)
    scored = models.IntegerField(blank=True, null=True)
    conceded = models.IntegerField(blank=True, null=True)
    founded = models.DateField(blank=True, null=True)

    games = models.IntegerField(blank=True, null=True)
    point = models.IntegerField(blank=True, null=True)
    average = models.IntegerField(blank=True, null=True)

    class Meta:
        ordering = [
            '-point', '-average'
        ]

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        self.games = self.win   self.draw   self.loss
        self.point = self.win*3   self.draw
        self.average = self.scored - self.conceded
        super(Team, self).save(*args, **kwargs)

it would be great if I utilize from slug item instead of id To make easy understand of which league you are in

urls.py

urlpatterns = [
    path('', home, name='home'),
    path(r'^/league/(?P<id>\d )/$', LeagueListView.as_view(), name='league'),
   

In order to learn what class based views is I should use them somehow once clicked in th base.html 's navbar, it should filter related league's teams

views.py

class LeagueListView(ListView):
    model = Team
    template_name = "league.html"
    context_object_name = "teams_of_league"

    # def get_queryset(self, request, *args, **kwargs):
    # HERE I want to get teams of specific league and send to the league.html

base.html

<ul >
                    <li >
                    <a  href="{% url 'league' specific_teams %}">Premier League</a>
                    </li>
                    <li >
                    <a  href="{% url 'league' specific_teams %}">La liga</a>
                    </li>
                    <li >
                    <a  href="{% url 'league' specific_teams %}">League 1</a>
                    </li>
                </ul>

league.html file

league.html

in this file there are table for corresponding league including teams informations

Thank you in advance

CodePudding user response:

urls.py

urlpatterns = [
    path('league/<id>', LeagueListView.as_view(), name='league'),
]

self.kwargs['id'] get the argument passed to the url: https://example.com/league/2

In this case we get 2.

You can see the Django documentation for more reference.

Here's an example of what your code should look like:

class LeagueListView(ListView):
    model = Team
    template_name = "league.html"
    context_object_name = "teams_of_league"

    def get_queryset(self): 
        league_id = int(self.kwargs['id'])
        self.league = League.objects.filter(pk=league_id).first()

Now that you have self.league instance, you can get the teams related to this league.

CodePudding user response:

Just Override the get_context method

def get_context_data(self,**kwargs):
    context = super(LeagueListView,self).get_context_data(**kwargs)
    league = League.objects.get(slug=kwargs.get('slug')) 
    context['teams']  = Team.objects.filter(league_id=league.id) 
    return context
  • Related