简体   繁体   English

我想在我的 Django 应用程序中限制借用对象

[英]I would like to limit the borrowing objects in my Django app

I have an app that displays games and user can borrow them.我有一个显示游戏的应用程序,用户可以借用它们。 I want to limit the amount of borrowed games for each user to max 3 games.我想将每个用户的借用游戏数量限制为最多 3 个游戏。 I created two models borrow and games.我创建了两个模型借用和游戏。 The methods work well except for the max borrowing.除了最大借用外,这些方法效果很好。

# Method display borrowed games
@login_required
def borrows(request):
    borrows = Borrow.objects.filter(owner = request.user).order_by('date_borrowed')
    context = {'borrows':borrows}
    return render(request, 'gameapp/borrows.html', context)`

# Methos allow the user borrow single game
@login_required
def borrow(request, boardgame_id):
    boardgame = Boardgame.objects.get(id=boardgame_id)
    if request.method != 'POST':
        form = BorrowForm()
    else:
        form = BorrowForm(data = request.POST)
        if form.is_valid():
            borrow_boardgame = form.save(commit = False)
            borrow_boardgame.owner = request.user
            # Replace the borrow varibale from borrow Model to main game
            borrow_boardgame.borrow = boardgame
            form.save()
            return redirect('gameapp:borrows')
    
    context = {'boardgame': boardgame, 'form':form}
    return render(request, 'gameapp/borrow.html', context)

I also used limit [:3] but this will keep the user sending data to the database in the backend.我还使用了 limit [:3] 但这将使用户将数据发送到后端的数据库。


# A model for the game
class Boardgame(models.Model):
    name = models.CharField(max_length=50)
    date_added = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now_add=True)

    owner = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

# A model for borrowing a game
class Borrow(models.Model):
    borrow = models.ForeignKey(Boardgame, on_delete=models.CASCADE)
    date_borrowed = models.DateTimeField(auto_now_add=True)
    date_returned = models.DateTimeField(auto_now_add=True)

    owner = models.ForeignKey(User, on_delete=models.CASCADE, default=3)

    def __str__(self):
        return str(self.borrow)```

You can check whether he has borrowed more than 3 games in form.is_valid() if like so:你可以在form.is_valid()中检查他是否借了超过 3 个游戏, if是这样:

if form.is_valid():
    borrowed_count = Borrow.objects.filter(owner=request.user).count()
    if borrowed_count < 4:
        form.save()
    else:
        print("You've borrowed 3 games already") # or whatever

Also that default in your owner field is specifying the user.pk not the games , you should change that.此外,您owner字段中的default是指定user.pk而不是games ,您应该更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python、django:我想将对象放在表单上的“输入”标签或“选择”标签内。 ..为了将对象转移到 booking_form - Python, django: I would like to put objects within the 'input' tag or 'select' tag on form. ..in order to transfer the objects to the booking_form 我想为Django DetailView添加条件 - I would like to add condition to Django DetailView Django ajax 如何更改我的代码以使用 ajax 在不刷新的情况下点赞帖子 - Django ajax How would I change my code to use ajax to like posts without refreshing 我将如何打包和销售 Django 应用程序? - How would I package and sell a Django app? 看起来日志崩溃了我的Django应用 - Looks like logs are crashing my Django app 我想让我的python将输出写入到csv文件吗? - I would like my python to write my output to a csv file? 我想获得 2 个相关 Django 模型中的行数 - I would like to get count of rows within 2 related Django Models 我想在 Django 中将视图中的数据操作到/渲染(html) - I would like to manipulate the data from the views to / render (html) in Django 我想在我的二十一点游戏中添加债务 - I would like to add Debt to my blackjack game 我想让我的程序显示订单总额/发票 - I would like to make my program display the order total/invoice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM