如何在 Django 中查询多对多字段?
How to query many-to-many fields in Django?
我正在尝试更新一个视图,在给定当前登录用户的情况下,我拥有 return 我的宠物 table 的所有物品。我在 Pet
中有一个多对多字段 petowners
.
PetOwner
模型如下...
class PetOwner(models.Model):
"""Model representing a pet owner."""
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=50, help_text="Enter owner's first name")
last_name = models.CharField(max_length=50, help_text="Enter owner's last name")
email = models.EmailField(
max_length=50, blank=True, null=True, unique=True, help_text="Enter owner's email"
)
phone_number = models.CharField(
max_length=15, blank=True, null=True, unique=True, help_text="Enter owner's phone number"
)
address = models.ForeignKey(
"Address", on_delete=models.SET_NULL, null=True, blank=True
)
Pet
模型如下...
class Pet(models.Model):
"""Model representing a pet."""
first_name = models.CharField(max_length=50, help_text="Enter pet's first name")
last_name = models.CharField(max_length=50, help_text="Enter pet's last name")
breeds = models.ManyToManyField("Breed", help_text="Select a breed for this pet")
weight = models.DecimalField(
max_digits=5, decimal_places=2, help_text="Enter pet's weight"
)
date_of_birth = models.DateField(null=True, blank=True)
date_of_death = models.DateField("Died", null=True, blank=True)
owners = models.ManyToManyField(PetOwner, help_text="Select an owner for this pet")
address = models.ForeignKey(
"Address", on_delete=models.SET_NULL, null=True, blank=True
)
我正在使用 Django 已经提供给我们的 User
进行身份验证(注册和登录)。 PetOwner
是在用户创建他们的帐户时创建的,连同他们的名字和姓氏。 PetOwner
只有 email
、phone_number
和 address
作为唯一字段,但不是必需的。因此,唯一性的保证来自 Django 的 User
模型中的 username
。
我不想 return 我数据库中的所有宠物,而是我只想 return 当前登录用户的宠物。如下所示...
Pet.objects.filter(owners.contain(request.user))
也许通过使用用户名?我不知道如何写这个查询。
首先设置owners
字段的related_name
:
class Pet(models.Model):
"""Model representing a pet."""
first_name = models.CharField(max_length=50, help_text="Enter pet's first name")
last_name = models.CharField(max_length=50, help_text="Enter pet's last name")
breeds = models.ManyToManyField("Breed", help_text="Select a breed for this pet")
weight = models.DecimalField(
max_digits=5, decimal_places=2, help_text="Enter pet's weight"
)
date_of_birth = models.DateField(null=True, blank=True)
date_of_death = models.DateField("Died", null=True, blank=True)
owners = models.ManyToManyField(PetOwner, help_text="Select an owner for this pet", related_name='pets') # <- Here
address = models.ForeignKey(
"Address", on_delete=models.SET_NULL, null=True, blank=True
)
那么你可以通过这个查询来获取用户的宠物:
target_user = User.objects.get(id=1) # This is only an example
pet_owner = PetOwner.objects.get(user=target_user)
pets = pet_owner.pets.all()
我正在尝试更新一个视图,在给定当前登录用户的情况下,我拥有 return 我的宠物 table 的所有物品。我在 Pet
中有一个多对多字段 petowners
.
PetOwner
模型如下...
class PetOwner(models.Model):
"""Model representing a pet owner."""
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=50, help_text="Enter owner's first name")
last_name = models.CharField(max_length=50, help_text="Enter owner's last name")
email = models.EmailField(
max_length=50, blank=True, null=True, unique=True, help_text="Enter owner's email"
)
phone_number = models.CharField(
max_length=15, blank=True, null=True, unique=True, help_text="Enter owner's phone number"
)
address = models.ForeignKey(
"Address", on_delete=models.SET_NULL, null=True, blank=True
)
Pet
模型如下...
class Pet(models.Model):
"""Model representing a pet."""
first_name = models.CharField(max_length=50, help_text="Enter pet's first name")
last_name = models.CharField(max_length=50, help_text="Enter pet's last name")
breeds = models.ManyToManyField("Breed", help_text="Select a breed for this pet")
weight = models.DecimalField(
max_digits=5, decimal_places=2, help_text="Enter pet's weight"
)
date_of_birth = models.DateField(null=True, blank=True)
date_of_death = models.DateField("Died", null=True, blank=True)
owners = models.ManyToManyField(PetOwner, help_text="Select an owner for this pet")
address = models.ForeignKey(
"Address", on_delete=models.SET_NULL, null=True, blank=True
)
我正在使用 Django 已经提供给我们的 User
进行身份验证(注册和登录)。 PetOwner
是在用户创建他们的帐户时创建的,连同他们的名字和姓氏。 PetOwner
只有 email
、phone_number
和 address
作为唯一字段,但不是必需的。因此,唯一性的保证来自 Django 的 User
模型中的 username
。
我不想 return 我数据库中的所有宠物,而是我只想 return 当前登录用户的宠物。如下所示...
Pet.objects.filter(owners.contain(request.user))
也许通过使用用户名?我不知道如何写这个查询。
首先设置owners
字段的related_name
:
class Pet(models.Model):
"""Model representing a pet."""
first_name = models.CharField(max_length=50, help_text="Enter pet's first name")
last_name = models.CharField(max_length=50, help_text="Enter pet's last name")
breeds = models.ManyToManyField("Breed", help_text="Select a breed for this pet")
weight = models.DecimalField(
max_digits=5, decimal_places=2, help_text="Enter pet's weight"
)
date_of_birth = models.DateField(null=True, blank=True)
date_of_death = models.DateField("Died", null=True, blank=True)
owners = models.ManyToManyField(PetOwner, help_text="Select an owner for this pet", related_name='pets') # <- Here
address = models.ForeignKey(
"Address", on_delete=models.SET_NULL, null=True, blank=True
)
那么你可以通过这个查询来获取用户的宠物:
target_user = User.objects.get(id=1) # This is only an example
pet_owner = PetOwner.objects.get(user=target_user)
pets = pet_owner.pets.all()