如何输出和保存django CreateView中相关的两个模型
我想展示和保存我的食谱。然而,我有一个配方模型,它与recipelist模型有多对多的关系 我的模型:如何输出和保存django CreateView中相关的两个模型,django,django-models,django-views,django-forms,Django,Django Models,Django Views,Django Forms,我想展示和保存我的食谱。然而,我有一个配方模型,它与recipelist模型有多对多的关系 我的模型: class IngredientList(models.Model): name = models.CharField(max_length=100) image = models.ImageField(default='ing.jpg', blank=True, null=True) recipes = models.ManyToManyField('RecipeL
class IngredientList(models.Model):
name = models.CharField(max_length=100)
image = models.ImageField(default='ing.jpg', blank=True, null=True)
recipes = models.ManyToManyField('RecipeList', through='RecipeList')
def __str__(self):
return self.name
class Units(models.Model):
name = models.CharField(max_length=20, default='')
unit = models.CharField(default=None, max_length=5)
def __str__(self):
return self.name
class RecipeList(models.Model):
choice = [
('Italian', 'It'),
('French', 'Fr'),
('Mexican', 'Mx'),
('Trinidad & Tobado', 'TT'),
]
name = models.CharField(max_length=100)
ingredients = models.ManyToManyField(
'IngredientList', through='ShoppingList')
instructions = models.TextField(max_length=3000)
serving_size = models.IntegerField()
cuisine_type = models.CharField(max_length=100, choices=choice)
image = models.ImageField(default='recipe.jpg', blank=True, null=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('recipe-detail', kwargs={'pk': self.pk})
class RecipeList(models.Model):
ingredients = models.ForeignKey(IngredientList, on_delete=models.CASCADE)
recipe = models.ForeignKey(RecipeList, on_delete=models.CASCADE)
unit = models.ForeignKey(
Units, on_delete=models.CASCADE, null=True, blank=True)
measurement = models.FloatField(default=1)
我的看法是:
class RecipeCreateView(CreateView):
model = RecipeList
fields = ['name', 'instructions', 'cuisine', 'serving_size', 'image']
我想在视图上输出一个表单,该表单具有:
配方名称
Recipe.RecipeList.measurement-Recipe.RecipeList.unit-Recipe.RecipeList.Components
配方说明
我不想输出Recipe.components,也不想定义每个元素的单位或测量值,而是希望能够将所有元素保存在一个表单中。
但我在数据库方面非常薄弱,而且对django还不熟悉。谁能给我指出正确的方向吗
from django.forms import ModelForm
from myapp.models import RecipeList
class RecipeForm(ModelForm):
class Meta:
model = RecipeList
fields = {'name', 'instructions', 'cuisine', 'serving_size', 'image'}
def somename(request):
# POST request
if request.method == POST:
#getting the post data
form = RecipeForm(request.POST, request.FILES)
# checking if the form is valid
if form.is_valid():
# saves the form
form.save()
# return to the same view
return redirect('somename')
#get request
else:
form = RecipeForm()
return render(request, 'templatesdirectory/htmlfile', {'form':form})
from django.forms import ModelForm
from myapp.models import RecipeList
class RecipeForm(ModelForm):
class Meta:
model = RecipeList
fields = {'name', 'instructions', 'cuisine', 'serving_size', 'image'}
def somename(request):
# POST request
if request.method == POST:
#getting the post data
form = RecipeForm(request.POST, request.FILES)
# checking if the form is valid
if form.is_valid():
# saves the form
form.save()
# return to the same view
return redirect('somename')
#get request
else:
form = RecipeForm()
return render(request, 'templatesdirectory/htmlfile', {'form':form})