如何在django模型中获取用户id?

发布于 2025-01-11 11:32:31 字数 725 浏览 0 评论 0原文

我需要从登录用户获取 id 来过滤模型中的查询。如何在 django 模型中获取用户 id? 感谢

models.py

class Portfo(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True)
    sell = models.ForeignKey(Sell, on_delete=models.CASCADE, null=True)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True)
    profit = models.PositiveIntegerField(null=True)
    final_amount = models.IntegerField(null=True)

    def __str__(self):
        return self.product.name

    def final_amount(self):
        final_amount = 0
        buy = Buy.objects.filter(product_id=self.product.id, owner__exact=...)
        sell = Sell.objects.filter(product_id=self.product.id)

I need to get id from logged user to filter query in models.how to get users id in django model?
Thanks

in models.py:

class Portfo(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True)
    sell = models.ForeignKey(Sell, on_delete=models.CASCADE, null=True)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True)
    profit = models.PositiveIntegerField(null=True)
    final_amount = models.IntegerField(null=True)

    def __str__(self):
        return self.product.name

    def final_amount(self):
        final_amount = 0
        buy = Buy.objects.filter(product_id=self.product.id, owner__exact=...)
        sell = Sell.objects.filter(product_id=self.product.id)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清欢 2025-01-18 11:32:31

我需要从登录用户获取 ID 来过滤模型中的查询。

模型被设计为不感知请求。如果要检索用户 ID,则需要通过参数传递它,因此:

class Portfo(models.Model):
    # …

    def final_amount(self, user_id):
        final_amount = 0
        buy = Buy.objects.filter(product_id=self.product_id, owner_id=user_id)
        sell = Sell.objects.filter(product_id=self.product_id, owner_id=user_id)
        # …

在视图中,您可以使用 .final_amount(…) 方法,例如:

from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404

@login_required
def some_view(request, pk):
    my_porfolio = get_object_or_404(Portfolio, pk=pk)
    final_amount = my_portfolio.final_amount(request.user.id)
    # …

I need to get id from logged user to filter query in models.

Models are designed to be request-unaware. If you want to retrieve the user id, you will need to pass it through a parameter, so:

class Portfo(models.Model):
    # …

    def final_amount(self, user_id):
        final_amount = 0
        buy = Buy.objects.filter(product_id=self.product_id, owner_id=user_id)
        sell = Sell.objects.filter(product_id=self.product_id, owner_id=user_id)
        # …

In the view, you can then use the .final_amount(…) method, for example with:

from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404

@login_required
def some_view(request, pk):
    my_porfolio = get_object_or_404(Portfolio, pk=pk)
    final_amount = my_portfolio.final_amount(request.user.id)
    # …
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文