0

I'm creating a game website and i have these models for games:

class Game(models.Model):
    accountant = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name='games')

    title = models.CharField(max_length=50)

    bank_money = models.IntegerField()
    player_starting_money = models.IntegerField()
    golden_card_amount = models.PositiveIntegerField(
        validators=[
            MinValueValidator(100),
            MaxValueValidator(1000)
        ]
    )

now i want every user to see their own games at dashbard:

class DashboardView(mixins.LoginRequiredMixin, generic.ListView):
    template_name = 'games/dashboard.html'
    model = models.Game

how can i do this (show every user their own games, not all games)?

1 Answer 1

2

You can override the get_queryset method for your DashboardView class to filter out only logged in user games.

class DashboardView(mixins.LoginRequiredMixin, generic.ListView):
    template_name = 'games/dashboard.html'
    model = models.Game

    def get_queryset(self):
        queryset = super().get_queryset().filter(accountant=self.request.user)
        return queryset
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.