首页 > 解决方案 > Django如何获得两个模型的关系?

问题描述

在我的 django 应用程序中,用户可以购买特定的发布元素。为了稍后显示 content_preview 或帖子的全部内容,我创建了一个“helper_model”-> Post_Paid_Sell_Tut 来检查用户是否为帖子付费。我的目标是稍后我可以根据付费或未付费状态显示 peview 或完整内容,但我不清楚如何在我的 view.py 中获得这两个模型的关系

我为此创建了以下两个模型:

现在我想了解如何在我的views.py中将这两个模型引入关系。最后,用户将看到一个只有购买按钮和一些文本的表单。在他点击按钮后,状态应该从未付费变为付费用户的付费。

我现在真的需要表格吗?如果是这样,它必须是什么样子?

感谢您的帮助:)

标签: pythondjangodjango-models

解决方案


首先,你应该稍微改变一下你的状态选择。

STATUSES = (
(0, 'unpaid'),
(1, 'paid')
)

class Post_Paid_Sell_Tut(models.Model):
    paying_user = models.ForeignKey(User, on_delete=models.CASCADE)
    post_sell_tut = models.ForeignKey(Post_Sell_Tut, on_delete=models.CASCADE)
    STATUS = models.IntegerField(choices=STATUSES, default=0)

这将简化以后的数据库搜索并澄清问题。然后,如果您只是更新状态,则不需要任何表格。

def post_sell_tut_buy(request, pk):
    post = get_object_or_404(Post_Sell_Tut, pk=pk)
    if request.user == post.author:
        messages.error(request, 'You cant buy your own Post!')
        return redirect('post_sell_tut_detail', pk=pk)
    else:
        template = 'Post_Sell_Tut/post_sell_tut_buy.html'
        post.STATUS = 1  # You are updating the STATUS
        post.save() # you are writing the change to database
        context = {
            'post': post
        }
        return render(request, template, context)

此外,通过将post实例传递给模板,您希望查看您应该遵循的信息:

def post_sell_tut_buy(request, pk):
    post = Post_Paid_Sell_Tut.objects.select_related('post_sell_tut').filter(pk=pk)
    if request.user == post.author:
        messages.error(request, 'You cant buy your own Post!')
        return redirect('post_sell_tut_detail', pk=pk)
    else:
        template = 'Post_Sell_Tut/post_sell_tut_buy.html'
        post.STATUS = 1  # You are updating the STATUS
        post.save() # you are writing the change to database
        context = {
            'post': post
        }
        return render(request, template, context)

我补充说select_related,因为当您尝试通过 post 中的外键获取信息时,它不会导致额外的 DB 请求

更新

def post_sell_tut_buy(request, pk):
    post = Post_Sell_Tut.objects.select_related('author).filter(pk=pk)
    if request.user == post.author:
        messages.error(request, 'You cannot buy POst(s) you created by your own!')
        return redirect('post_sell_tut_detail', pk=post.pk)
    else:
        template = 'Post_Sell_Tut/post_sell_tut_buy.html'
        context = {
            'post': post,
        }
        return render(request, template, context)

您无法STATUSPost_Sell_Tut模型访问。它没有那个字段。

def post_sell_tut_buy_exec(request, pk):
    post_sell_tut = get_object_or_404(Post_Sell_Tut, pk=pk)
    post_paid_sell_tut = Post_Paid_Sell_Tut.objects.filter(post_sell_tut=post_sell_tut)
    if request.user == post.author:
        messages.error(request, 'You cannot buy Post(s) you created by your own!')
        return redirect('post_sell_tut_detail', pk=post.pk)
    else:
        template = 'Post_Sell_Tut/post_sell_tut_buy.html'
        post_paid_sell_tut.STATUS = 1
        post_paid_sell_tut.save()
        context = {
            'post': post
        }
        return render(request, template, context)

推荐阅读