I have an app that displays games and users can borrow them. I want to limit the amount of borrowed games for each user to max 3 games. I created two models borrow and games. The methods work well except for the max borrowing.
Advertisement
Answer
You can check whether he has borrowed more than 3 games in form.is_valid()
if
like so:
JavaScript
x
7
1
if form.is_valid():
2
borrowed_count = Borrow.objects.filter(owner=request.user).count()
3
if borrowed_count < 4:
4
form.save()
5
else:
6
print("You've borrowed 3 games already") # or whatever
7
Also that default
in your owner
field is specifying the user.pk
not the games
, you should change that.