【问题标题】:how to use filter in query twice in django如何在 Django 中两次在查询中使用过滤器
【发布时间】:2022-01-11 13:42:38
【问题描述】:

我正在尝试使用此查询过滤一些数据,

get_members = PaymentDetails.objects.filter(participants_name=Participants.objects.filter(participants_name=Profile.objects.get(user=request.user)))

但我收到此错误精确查找的 QuerySet 值必须限制为使用切片的一个结果。我的模型看起来像这样

class Committee(models.Model):
    committee_creator = models.ForeignKey(Profile, on_delete=models.CASCADE)
    committee_name = models.CharField(max_length=100)

class Participants(models.Model):
    participants_name = models.ForeignKey(Profile, on_delete=models.CASCADE)
    participants_committee_name = models.ForeignKey(Committee, on_delete=models.CASCADE)
   
class PaymentDetails(models.Model):
    participants_name = models.ForeignKey(Participants, on_delete=models.CASCADE)
    participants_paid_status = models.BooleanField(default=False)
    participants_amount_paid = models.IntegerField()

【问题讨论】:

    标签: django database model


    【解决方案1】:

    试试这个我会假设你在UserProfile之间有OneToOne关系。

    get_members = PaymentDetails.objects.filter(participants_name__participants_name_id = request.user.profile.pk)
    

    【讨论】:

    • 我在数据库中有两个结果,但它只显示其中一个已登录的用户,
    • @faizkhan 您想为所有用户获取吗?您显示的代码仅适用于当前用户。
    • 我想获取所有属于 paymentdetails 模型的用户
    • @faizkhan 试试这个PaymentDetails.objects.all().values_list('participants_name__participants_name__user',flat=True)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    相关资源
    最近更新 更多