0
我是新來的django,我正在製作食譜食譜應用程序。我希望用戶能夠添加他們自己的配方。添加多個字段Django的形式
models.py
from django.db import models
from django.core.urlresolvers import reverse
class Recipe(models.Model):
def __unicode__(self):
return self.recipe_name
recipe_name = models.CharField(max_length=250)
category = models.CharField(max_length=50)
description = models.CharField(max_length=1000)
image = models.CharField(max_length=1000)
prep_time = models.CharField(max_length=250)
difficulty = models.CharField(max_length=50)
instructions_url = models.CharField(max_length=1000)
def get_absolute_url(self):
return reverse('detail', kwargs={'pk': self.pk})
class Ingredients(models.Model):
def __unicode__(self):
return self.ingredients
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
ingredients = models.CharField(max_length=1000)
views.py
class RecipeCreate(CreateView):
model = Recipe
fields = ['recipe_name', 'category', 'image', 'prep_time', 'difficulty', 'instructions_url']
此刻我的表格只顯示來自我的「類配方」的領域,但我想要的是有成分場選項可添加其他字段。你有什麼建議如何做到這一點?謝謝!
見我的回答如下。注意:我會將'Ingredients'模型更改爲單數。每個「成分」都有自己的db行。或者你是否真的想把食譜的所有成分都保存爲一個長長的CharField? – dirkgroten