Django如何查询与“列表”对象关联的所有用户

2024-09-27 09:25:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我的模型是这样的:

  • CustomUser与“Listing”对象具有多对多关系

  • 每个列表对象都有一个“author”字段

    author = models.ForeignKey(CustomUser)
    
  • 本质上,一个列表既有作者(CustomUser)也有需要确认的自定义用户。

我正在尝试让所有需要确认作者发布的所有列表的自定义用户。你知道吗

我不知道正确的查询方式是什么。你知道吗

Listing.objects.filter(author=author).needsConfirmation_set.all()

模型

class CustomUser(models.Model):
    user = models.OneToOneField(User)
    location = models.CharField(max_length=50)
    rating = models.FloatField(null=True)
    level = models.CharField(max_length=50)
    followers = models.ManyToManyField("self", related_name="followers")
    following = models.ManyToManyField("self", related_name="following")
    interested = models.ManyToManyField('Listing', related_name = 'interested', default=None)
    needsConfirmation = models.ManyToManyField('Listing', related_name = 'needsConfirmation', default=None)
    confirmed = models.ManyToManyField('Listing', related_name = 'confirmed', default=None)

class Listing(models.Model): 
    subject = models.CharField(max_length=42)
    location = models.CharField(max_length=50, default="N/A")
    description = models.CharField(max_length=500)
    author = models.ForeignKey(CustomUser)
    date = models.DateTimeField(default=timezone.now)

Tags: name模型nonedefault列表modelslengthmax

热门问题