如何在 django 中没有循环的情况下获取 foreighkey 的 foreighkey?
how do I get foreighkey of foreighkey without a loop in django?
这是一个 Django 项目,我正在尝试创建一个心愿单(多对多无济于事,因为我需要在心愿单中获取该心愿单的 DateTime)。
class Client(models.Model):
name = models.CharField(max_length=100)
user = models.ForeignKey(User, on_delete=models.CASCADE)
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField()
class WishItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
client = models.ForeignKey(Client, related_name="wishlist", on_delete=models.CASCADE)
added_at = models.DateTimeField(auto_now_add=True)
我能做的只有这个:
wishlist = Client.objects.wishlist.select_related('product').all()
wish_products = [item.product for item in wishlist]
但我需要这样的东西,没有循环但有一个 SQL 查询和单行
wishlist = Client.objects.wishlist.product.all()
当我尝试 运行 这段代码时,出现错误 AttributeError: 'RelatedManager' object has no attribute 'product'
多对多关系可以解决您可以向 WishItem 添加额外字段的问题 class 您可以试试这个:
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField()
class Client(models.Model):
name = models.CharField(max_length=100)
user = models.ForeignKey(User, on_delete=models.CASCADE)
WishProducts = models.ManyToManyField(Product,through='WishItem')
class WishItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
client = models.ForeignKey(Client, on_delete=models.CASCADE)
added_at = models.DateTimeField(auto_now_add=True)
您可以 .filter(…)
[Django-doc] and then .order_by(…)
[Django-doc] 使用:
Product.objects.filter(<b>wishitem__client__user=<i>my_user</i></b>).order_by(<b>'wishitem__added_at'</b>)
您可以将 ManyToManyField
与您的 WishItem
:
class Client(models.Model):
# …
wishlist = models.<strong>ManyToManyField(</strong>
'Product',
<strong>through='WishItem'</strong>
<strong>)</strong>
class Product(models.Model):
# …
pass
class WishItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
client = models.ForeignKey(Client, related_name='wishitems', on_delete=models.CASCADE)
added_at = models.DateTimeField(auto_now_add=True)
那么你可以查询:
Product.objects.filter(<b>client__user=<i>my_user</i></b>).order_by(<b>'wishitem__added_at'</b>)
这也将使查询 Client
的 .wishlist
更加方便,Product
的 .client_set
是管理 Client
有 Product
在心愿单上。
Note: It is normally better to make use of the settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use the User
model [Django-doc] directly. For more information you can see the referencing the User
model section of the documentation.
这是一个 Django 项目,我正在尝试创建一个心愿单(多对多无济于事,因为我需要在心愿单中获取该心愿单的 DateTime)。
class Client(models.Model):
name = models.CharField(max_length=100)
user = models.ForeignKey(User, on_delete=models.CASCADE)
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField()
class WishItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
client = models.ForeignKey(Client, related_name="wishlist", on_delete=models.CASCADE)
added_at = models.DateTimeField(auto_now_add=True)
我能做的只有这个:
wishlist = Client.objects.wishlist.select_related('product').all()
wish_products = [item.product for item in wishlist]
但我需要这样的东西,没有循环但有一个 SQL 查询和单行
wishlist = Client.objects.wishlist.product.all()
当我尝试 运行 这段代码时,出现错误 AttributeError: 'RelatedManager' object has no attribute 'product'
多对多关系可以解决您可以向 WishItem 添加额外字段的问题 class 您可以试试这个:
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField()
class Client(models.Model):
name = models.CharField(max_length=100)
user = models.ForeignKey(User, on_delete=models.CASCADE)
WishProducts = models.ManyToManyField(Product,through='WishItem')
class WishItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
client = models.ForeignKey(Client, on_delete=models.CASCADE)
added_at = models.DateTimeField(auto_now_add=True)
您可以 .filter(…)
[Django-doc] and then .order_by(…)
[Django-doc] 使用:
Product.objects.filter(<b>wishitem__client__user=<i>my_user</i></b>).order_by(<b>'wishitem__added_at'</b>)
您可以将 ManyToManyField
与您的 WishItem
:
class Client(models.Model):
# …
wishlist = models.<strong>ManyToManyField(</strong>
'Product',
<strong>through='WishItem'</strong>
<strong>)</strong>
class Product(models.Model):
# …
pass
class WishItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
client = models.ForeignKey(Client, related_name='wishitems', on_delete=models.CASCADE)
added_at = models.DateTimeField(auto_now_add=True)
那么你可以查询:
Product.objects.filter(<b>client__user=<i>my_user</i></b>).order_by(<b>'wishitem__added_at'</b>)
这也将使查询 Client
的 .wishlist
更加方便,Product
的 .client_set
是管理 Client
有 Product
在心愿单上。
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.