当前位置: 首页 > 知识库问答 >
问题:

Django生成相反模型列表

陶博耘
2023-03-14

我正试图开发一个程序,将确定哪些植物对其他植物有益和有害。我希望它能够遍历一个植物列表,比较并查看一种植物的detis_insects_and_imports物种是否与另一种植物的pestors,然后它将其添加到ally_dete_pests_for(self):中的列表。

     class AnimalSpecies(models.Model):
            common_name = CharField(max_length = 200, null = True, blank = True)
            scientific_name = CharField(max_length = 200, null = True, blank = True)
        
            genus = Foreign

Key(Genus, null = True, blank = True, on_delete = models.CASCADE)
        class Meta:
            verbose_name = "Animal Species"
            verbose_name_plural = "Animal Species"
        def __str__(self):
            return self.common_name
    #___________________________________________________________________________________Begin Species_______________________________________________
    class PlantSpecies(models.Model):
        #________________________Name & Relationships________________________
        common_name = CharField(max_length = 200, null = True, blank = True)
        species_name = CharField(max_length = 200, null = True, blank = True)
        genus = ForeignKey(Genus, blank = True, null =True, on_delete = models.CASCADE)
        rotation_family = ForeignKey(RotationFamily, blank = True, null = True, on_delete = models.CASCADE)
    
        #________________________Growth & Environment________________________
        annual = BooleanField(null = True, blank = True)
        GROWTH_HABIT_LIST = [
            ("H", "Herb"),
            ("S", "Shrub"),
            ("T", "Tree"),
            ("U", "Succulent"),
            ("G", "Grass"),
            ("F", "Fern"),
            ("V", "Vine")
        ]
        growth_habit = CharField(max_length = 20, blank = True, null = True, choices = GROWTH_HABIT_LIST)
        pruning = TextField(max_length = 1000, null = True, blank = True)
        days_to_germinate = IntegerField(null = True, blank = True, default = 0)
        days_to_maturity = IntegerField(null = True, blank = True, default = 0)
        zone = IntegerField(null = True, blank = True, default = 0)
    
        SUN_REQUIREMENT_LIST = [
            ("FH", "Full Shade"),
            ("FHPH", "Full Shade-Partial Shade"),
            ("PHFS", "Partial Shade-Full Sun"),
            ("FS", "Full Sun")
        ]
        sun_requirement = CharField(max_length = 200, null = True, blank = True, choices = SUN_REQUIREMENT_LIST)
        WATER_REQUIREMENT_LIST = [
            ("M", "Mesic"),
        ]
        water_requirement = CharField(max_length = 20, null = True, blank = True, choices = WATER_REQUIREMENT_LIST)
    
    
        pollinator = ManyToManyField(AnimalSpecies, blank = True, related_name = "pollinators")
        beneficials = ManyToManyField(AnimalSpecies, blank = True, related_name = "beneficials")
        pests = ManyToManyField(AnimalSpecies, blank = True, related_name = "Pests")
    
        deters_insect_and_animals = ManyToManyField(AnimalSpecies, blank = True, related_name = "deters_AnimalSpecies")
        
        #________________________Spacing________________________
        number_per_square_foot = IntegerField(null = True, blank = True, default = 0)
        spacing_inches = FloatField(max_length = 200, null = True, blank = True, default = 0)
        spread_inches = FloatField(max_length = 200, null = True, blank = True, default = 0)
        height = IntegerField(null = True, blank = True, default = 0)
        #________________________Yield________________________
        expected_yield_pounds = FloatField(max_length = 200, blank = True, null = True, default = 0)
        expected_pound_per_fruit = FloatField(max_length = 200, blank = True, null = True, default = 0)
        #________________________Description________________________
        COLOR_CHOICES = [
            ("RE", "Red"),
            ("OR", "Orange"),
            ("YE", "Yellow"),
            ("LGR", "Light Green"),
            ("GR", "Green"),
            ("DGR", "Dark Green"),
            ("BL", "Blue"),
            ("PU", "Purple"),
            ("PI", "Pink"),
            ("WH", "White")
        ]
       
        
        foliage_color = CharField(max_length = 20, null = True, blank = True, choices = COLOR_CHOICES)
        flower_color = CharField(max_length = 20, null = True, blank = True, choices = COLOR_CHOICES)
        fruit_color = CharField(max_length = 20, null = True, blank = True, choices = COLOR_CHOICES)
    
        PARTS_CHOICES = [
            ("FLRW", "Fruit, Leaves, Roots, Flowers"),
            ("FLW", "Fruit, Leaves, Flowers"),
            ("FR", "Fruit, Roots, Flowers"),
            ("LR", "Leaves, Roots, Flowers"),
            ("LRW", "Leaves, Roots, Flowers"),
            ("FL", "Fruit, Leaves"),
            ("FR", "Fruit, Roots"),
            ("LR", "Leaves, Roots"),
            ("F", "Fruit"),
            ("L", "Leaves"),
            ("R", "Roots"),
            ("W", "Flowers"),
            ("O", "Other"),
            ("N", "None")
        ]
        edible_parts = CharField(max_length = 20, null = True, blank = True, choices = PARTS_CHOICES)
        toxic_parts = CharField(max_length = 20, null = True, blank = True, choices = PARTS_CHOICES)
        @property
        def improves_growth_and_flavor(self):
            return ManyToManyField(self, blank = True, related_name = "improves_growth")
        
        @property
        def improves_health_and_flavor(self):
            return ManyToManyField(self, blank = True, related_name = "improves_health")
        @property
        def impairs_health_and_growth(self):
            return ManyToManyField(self, blank = True, related_name = "impairs_health")
            
        @property
        def visual_name(self):
            return f"{self.rotation_family.visual_color}{self.common_name}"
        visual_name.fget.short_description = "Name"
        @property
        def scientific_name(self):
            return f"{self.genus.scientific_name} {self.species_name}"
        @property
        def expected_fruit_yield(self):
            try:
                return self.expected_yield_pounds / self.expected_pound_per_fruit
            except:
                return "0"
    
        @property
        def ally_deter_pest_for(self):
            x = []
            y = PlantSpecies.objects.filter(self.pests)
            for i in range(len(self.deters_insect_and_animals)):
                for h in range(len(y)):
                    if self.deters_insect_and_animals[i] == y[h]:
                        x.append(f"{y[h].common_name} | {self.deters_insect_and_animals[i]}")
                    else:
                        pass
            return x

内部服务器错误:/admin/gardenapp/plantmonics/Traceback(最近的调用为last):文件“C:\users\deant\appdata\local\programs\python\python39\lib\site-packages\django\db\models\options.py”,第575行,在get_field返回self.fields_map[field_name]键错误:“ally_dete_pest_for”

在处理上述异常的过程中,发生了另一个异常:

回溯(最近的调用最后):文件“C:\users\deant\appdata\local\programs\python\python39\lib\site-packages\django\contrib\admin\utils.py”,第265行,在lookup_field f=_get_non_gfk_field(opts,name)文件“b\site-packages\django\db\models\options.py”,第577行,在get_field中引发fielddoesnotext(“%s没有名为'%s'的字段”%(self.object_name,field_name))django.core.exceptions.fielddoesnotext:plantvements没有名为'ally_dete_pest_for'的字段

在处理上述异常的过程中,发生了另一个异常:

共有1个答案

齐典
2023-03-14

您首先需要获得所有相关的对象。

for i in range(len(self.deters_insect_and_animals.all())):
    # further code

参考:ManyToMany关系

 类似资料:
  • 生成数据库中所有表的模型文件,如果设置了include或exclude,则按照相应规则过滤表。 必选参数: -namespace 生成的Model所在命名空间 可选参数: -database 数据库名,不传则取连接池默认配置的库名 -baseClass 生成时所继承的基类(自行实现务必继承框架的模型类),默认Imi\Model\Model,可选 -poolName 连接池名称,不传则取默认连接池

  • 监督学习的任务就是从数据中学习一个模型(也叫分类器),应用这一模型,对给定的输入X预测相应的输出Y。这个模型的一般形式为决策函数Y=f(X)或者条件概率分布P(Y|X)。 ** 决策函数Y=f(X):**你输入一个X,它就输出一个Y,这个Y与一个阈值比较,根据比较结果判定X属于哪个类别。例如两类(w1和w2)分类问题,如果Y大于阈值,X就属于类w1,如果小于阈值就属于类w2。这样就得到了该X对应的

  • 问题内容: 我在家里一直在使用Django和Django ORM,我不得不说,就易用性而言,它是目前最好的之一。 但是,我想知道是否可以在“反向”中使用它。 基本上,我想做的是从现有的数据库模式(从不使用django且相当老的项目中)生成Django模型。 这可能吗? 更新:有问题的数据库是Oracle 问题答案: 是的,使用命令: http://docs.djangoproject.com/en

  • 问题内容: 如何刷新Django模板中的某个元素? 例: 可以说页面中的其他元素触发了一个应刷新上面div的javascript。有没有办法让django刷新模板中的这个特定元素? 如果没有,我将不得不使用常规的JS或jQuery方法来对div进行猴子补丁,而不要使用django模板层的强大功能。另外,上面的代码是实际模板的简化,我使用了模板的大部分功能,因此猴子修补生成的html将是一场噩梦…

  • 本文向大家介绍生成式模型、判别式模型相关面试题,主要包含被问及生成式模型、判别式模型时的应答技巧和注意事项,需要的朋友参考一下 https://github.com/imhuay/Algorithm_Interview_Notes-Chinese/blob/master/A-机器学习/A-机器学习基础.md#生成模型与判别模型 生成式模型(generative model)会对x和y的联合分布p(

  • 我目前正在进行一个项目,该项目需要使用QVTO将一个模型转换为另一个模型。源模型是以表格形式表示的状态机。目标模型也是具有节点和边的statemachine。源模型只有领域元模型,但目标模型同时具有领域和符号元模型。符号元模型是http://www.eclipse.org/gmf/runtime/1.0.2/notation。我从源域模型转换到目标域模型,但是我的目标模型没有表示法模型。我想做的是