简体   繁体   English

Django ManyToMany 传递变量

[英]Django ManyToMany passing variables

I have started my Django project and I want to share some data between 2 classes.我已经开始了我的 Django 项目,我想在 2 个类之间共享一些数据。 I'm not sure if I am doing it correctly.我不确定我是否做对了。 It works but i don't want to populate in my project bad practices.它有效,但我不想在我的项目中出现不良做法。 My code looks like this:我的代码如下所示:

class Products(models.Model):
    name = models.CharField(max_length=50)
    protein = models.FloatField()
    carbohydrates = models.FloatField()
    fat = models.FloatField()
    food_type = models.CharField(max_length=6, choices=(
        ("1", "Meat"),
        ("2", "Fruit")
        )
    )
class Meals(models.Model):#Child
    name = models.CharField(max_length=50)
    ingredient = models.ManyToManyField(Products)

    def protein(self):
        protein = 0
        for ing in self.ingredient.all():
            protein += ing.protein
        return protein 

    @property
    def carbohydrates(self):
        carbohydrates = 0
        for ing in self.ingredient.all():
            carbohydrates += ing.carbohydrates
        return carbohydrates 

    def diet_category(self):        
        diet_types = "vegan, vegeterian, Keto, Paleo, Gluten-free"
        food_types = ""
        for ing in self.ingredient.all():
            food_types += ing.food_type
        if "1" in food_types: 
            diet_types.replace("vegan, ", "").replace(" vegeterian,", "")
        return (diet_types + " | " + food_types)

Additionally I have problem with .replace() function in python, which i want to use here to exclude some words from variable.另外,我在 python 中使用 .replace() 函数有问题,我想在这里使用它从变量中排除一些单词。

Summing up my questions are:总结我的问题是:

-Retrieving properties from another class is done by referring to an object of that class. - 从另一个类中检索属性是通过引用该类的对象来完成的。 In this case Products.objects.all()在这种情况下 Products.objects.all()

-How can I remove words from variable in models. - 如何从模型中的变量中删除单词。

-Should i use @property for functions which are returning values from another class? - 我应该对从另一个类返回值的函数使用 @property 吗?

Replace()代替()

The replace() function returns a copy of the string with the replaced values. replace()函数返回带有替换值的字符串副本。 It will not change the original string.它不会改变原始字符串。

So you need to do something like所以你需要做类似的事情

diet_types = diet_types.replace("vegan, ", "")
diet_types = diet_types.replace("vegetarian, ", "")

Fetching values取值

You can loop like you do or use a query to achieve the same, somthing like:您可以像这样循环或使用查询来实现相同的效果,例如:

def protein(self):
    return self.ingredient.aggregate(Sum('protein'))['protein__sum']

See the docs on aggregation for more details.有关更多详细信息,请参阅有关聚合的文档。

Properties特性

IMHO it is a matter of taste恕我直言,这是一个品味问题

Your classes can have properties dependent on other classes but make sure that you handle exception where there are no values found in other class.您的类可以具有依赖于其他类的属性,但请确保您处理在其他类中找不到值的异常 So I believe using @property is not a bad idea .所以我相信使用@property 不是一个坏主意

Moreover, did you try using此外,您是否尝试使用

diet_types.replace("vegan, ", " vegeterian")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM