I am completely new to Django, so forgive me if this is the wrong approach. While I'm trying to learn Django, I like to do my own little practice problems to solidify my understanding.
Working with models I created the example model objects:
from django.db import models
class Food(models.Model):
name = models.CharField(
max_length=50,
help_text="Name of Fruit or Vegetable"
)
food_group = models.CharField(
max_length=9,
class Vitamins(models.Model):
name = models.ForeignKey(
Food,
on_delete=models.CASCADE
)
vitamin_A = models.CharField(
max_length=20,
)
.
.
.
folate = models.CharField(
max_length=20,
help_text="Folic Acid"
)
In my object, I have 5 identical fields that store text ie (Trace amounts, 5mg, No Information). However it seems tedious to type out every field. Is there a more efficient way to do this?
I think you should take advantage of the relational database and create a model for Vitamin, with name and folate, something like this
from django.db import models
class Food(models.Model):
name = models.CharField(
max_length=50,
help_text="Name of Fruit or Vegetable"
)
class Vitamin(models.Model):
food = models.ForeignKey(
Food,
on_delete=models.CASCADE
)
name = models.CharField(
max_length=20,
)
folate = models.CharField(
max_length=20,
help_text="Folic Acid"
)
And then for every Vitamin in the Food you create a record. It can be 1, 2 or N. This is very flexible and scaleable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.