简体   繁体   English

Django - 如何从具有外键的 model 中获取所有项目?

[英]Django - How to get all items from a model that has foreign key?

I'm creating two models: Feedstockformula and formula.我正在创建两个模型:Feedstockformula 和 formula。 This is the code for each one:这是每个代码:

FeedstockFormula:原料配方:

from django.db import models

class FeedstockFormulas(models.Model):
    ratio = models.FloatField()
    feedstock = models.OneToOneField("dashboard.Feedstock", on_delete=models.CASCADE, default="0")
    formulas = models.ForeignKey("dashboard.Formulas", on_delete=models.CASCADE, default="")

Formula:公式:

from django.db import models

class Formulas(models.Model):
    name = models.CharField(max_length=100)
    cost = models.FloatField()
    weight = models.FloatField()

How can I get all the FeedstockFormulas that is part of a Formula?我怎样才能获得作为配方一部分的所有原料配方?

You should add "related_name" to formulas field in FeedstockFormulas.您应该将“related_name”添加到 FeedstockFormulas 中的公式字段。

Like this:像这样:

formulas = models.ForeignKey("dashboard.Formulas", related_name="feed_stock_formulas",on_delete=models.CASCADE, default="")

Django use reverse relation on ForeignKey, you can access all FeedstockFormulas that is part of a Formula by writing this: Django 在 ForeignKey 上使用反向关系,您可以通过编写以下代码访问作为公式一部分的所有 FeedstockFormulas:

Formulas.feed_stock_formulas

You can access the FeedstockFormulas via the related_name of your formulas field, which defaults to feedstockformulas_set in your case.您可以通过formulas字段的related_name FeedstockFormulas在您的情况下默认为feedstockformulas_set

For example:例如:

formula = Formulas.objects.first()
print(formula.feedstockformulas_set.all().feedstock)

You could do it like this你可以这样做

First get a reference to the formula you want with something like首先参考你想要的公式

f = Formula.objects.get(name = <formula name>)

Then you use it to get the feedstockformulas然后你用它来得到原料配方

fsf = FeedstockFormulas.objects.filter(formulas = f)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取 django 中与外键相关的所有项目? - How to get all items related to a Foreign Key in django? 如何在外键 Django 中获取计数项目? - How to get count items in foreign key Django? 如何在 django 的详细视图中使用外键获取连接到另一个 model 的 model 中的项目? - How to Get Items in a model connected to another model using a foreign key in detail view in django? 如何从外键访问model,Django? - How to access model from Foreign Key, Django? 如何在Django中获取子模型中存在外键的父模型的所有对象? - How to get all objects of a Parent model of which foreign key exist in child model in Django? Django:如何从外键和下一个外键中获取值? - Django: How to get value from foreign key and next foreign key? 如何在django rest框架中获取所有字段,该框架在带有外键的不同模型中被定义为相关名称? - How to get all fields in django rest framework which is defined as related name in a diffent model with foreign key? 在 Django 中获取外键的所有外键 - Get all Foreign Keys of a Foreign Key in Django Django模型没有设置外键对象 - Django model has no foreign key object set 如何在具有用户 model 外键的 model 中定义 __str__() 方法并获取 Django 中的字段值? - How do I define __str__() method in a model which has a Foreign Key of User model and get the values of fields in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM