How can I do "presets" in Django models -
i create store of products.
class product(models.model): name = models.charfield(verbose_name=_(u'name'), max_length=200) quantity = models.integerfield(verbose_name=_(u'quantity')) >>> product.objects.create(name="egg", quantity="100") >>> product.objects.create(name="ham", quantity="10") now want create recipes, like: scrambled eggs 3 eggs , 1 piece of ham. tried describe like:
class recipe(models.model): name = models.charfield(verbose_name=_(u'name'), max_length=200) items = models.manytomanyfield('product', related_name='recipe_sets') but stuck in description number of required products 1 recipe.
should put quantity in other model? how can calculate number of servings (depending on quantity of product)? there elegant way or application deduction of products (as result of cooking 1 dish)?
you're going need table. manytomany create intermediate table linking 2 models have. if you're more explicit in how table created, can add fields onto linking table.
see documentation extra fields on many many relationships. basically, you'd have model:
class ingredients(models.model): product = models.foreignkey(product) recipe = models.foreignkey(recipe) quantity = models.somefield(..) and change recipe definition manytomanyfield:
items = models.manytomanyfield('product', related_name='recipe_sets', through='ingredients')
Comments
Post a Comment