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)?
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