I am trying to develop a program that will determine what plants are beneficial and harmful to other plants. I want it to be able to run through a list of plants and compare and see if one plant's deters_insects_and_animals
species matches the other plant's pests
then it adds it to the list in ally_deter_pests_for(self):
I'm not really sure how to do that.
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
For example, it would find that marigolds deter flea beetles, and broccoli is affected by flea beetles. Therefore, marigolds ally_deter_pest_for(self):
would x.append(broccoli)
Right now I get an error
Internal Server Error: /admin/GardenApp/plantspecies/ Traceback (most recent call last): File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\options.py", line 575, in get_field return self.fields_map[field_name] KeyError: 'ally_deter_pest_for'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\utils.py", line 265, in lookup_field f = _get_non_gfk_field(opts, name) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\utils.py", line 296, in _get_non_gfk_field field = opts.get_field(name) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\options.py", line 577, in get_field raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name)) django.core.exceptions.FieldDoesNotExist: PlantSpecies has no field named 'ally_deter_pest_for'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner response = get_response(request) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 202, in _get_response response = response.render() File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\response.py", line 105, in render self.content = self.rendered_content File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\response.py", line 83, in rendered_content return template.render(context, self._request) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\backends\django.py", line 61, in render return self.template.render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 170, in render return self._render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 162, in _render return self.nodelist.render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 938, in render bit = node.render_annotated(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 905, in render_annotated return self.render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\loader_tags.py", line 150, in render return compiled_parent._render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 162, in _render return self.nodelist.render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 938, in render bit = node.render_annotated(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 905, in render_annotated return self.render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\loader_tags.py", line 150, in render return compiled_parent._render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 162, in _render return self.nodelist.render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 938, in render bit = node.render_annotated(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 905, in render_annotated return self.render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\loader_tags.py", line 62, in render result = block.nodelist.render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 938, in render bit = node.render_annotated(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 905, in render_annotated return self.render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\loader_tags.py", line 62, in render result = block.nodelist.render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 938, in render bit = node.render_annotated(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 905, in render_annotated return self.render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\templatetags\base.py", line 33, in render return super().render(context) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\library.py", line 214, in render _dict = self.func(*resolved_args, **resolved_kwargs) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\templatetags\admin_list.py", line 341, in result_list 'results': list(results(cl)), File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\templatetags\admin_list.py", line 317, in results yield ResultList(None, items_for_result(cl, res, None)) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\templatetags\admin_list.py", line 308, in init super().init(*items) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\templatetags\admin_list.py", line 233, in items_for_result f, attr, value = lookup_field(field_name, result, cl.model_admin) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\utils.py", line 276, in lookup_field attr = getattr(obj, name) File "C:\Users\deant\OneDrive\Documents\Django\Aegirsoft_Garden\GardenApp\models.py", line 201, in ally_deter_pest_for y = PlantSpecies.objects.filter(self.pests) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 942, in filter return self._filter_or_exclude(False, *args, **kwargs) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 962, in _filter_or_exclude clone._filter_or_exclude_inplace(negate, *args, **kwargs) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 969, in _filter_or_exclude_inplace self._query.add_q(Q(*args, **kwargs)) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\query.py", line 1358, in add_q clause, _ = self._add_q(q_object, self.used_aliases) File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\query.py", line 1377, in _add_q child_clause, needed_inner = self.build_filter( File "C:\Users\deant\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\query.py", line 1255, in build_filter arg, value = filter_expr TypeError: cannot unpack non-iterable ManyRelatedManager object [01/Sep/2021 01:51:57] "GET /admin/GardenApp/plantspecies/ HTTP/1.1" 500 400341