[英]ModelForms with ManyToMany relationship Models in Django
I have a question on ModelForms in Django. 我对Django中的ModelForms有疑问。 If I'm creating a form from a model using ModelForms, then how the form fields will be linked to these M2M relationships ?
如果我正在使用ModelForms从模型创建表单,那么表单字段将如何链接到这些M2M关系? what I mean is if I have :
我的意思是,如果我有:
class Recipe(models.Model):
title = models.CharField(max_length=100)
category = models.ForeignKey(Category)
cuisineType = models.ForeignKey(CuisineType)
description = models.TextField()
serving = models.CharField(max_length=100)
cooking_time = models.TimeField()
ingredients = models.ManyToManyField(RecipeIngredient)
directions = models.TextField()
image = models.OneToOneField(Image)
added_at = models.DateTimeField(auto_now_add=True)
last_update = models.DateTimeField(auto_now=True)
added_by = models.ForeignKey(UserProfile, null=False)
tags = models.ManyToManyField(Tag,blank=True)
class Category(models.Model):
category = models.CharField(max_length=100)
category_english = models.CharField(max_length=100)
#slug = models.SlugField(prepopulate_from=('name_english',))
slug = models.SlugField()
parent = models.ForeignKey('self', blank=True, null=True, related_name='child')
description = models.TextField(blank=True,help_text="Optional")
class Meta:
verbose_name_plural = 'Categories'
#sort = ['category']
class RecipeForm(ModelForm):
class Meta:
model = Recipe
exclude = ('added_at','last_update','added_by')
def RecipeEditor(request, id=None):
form = RecipeForm(request.POST or None,
instance=id and Recipe.objects.get(id=id))
# Save new/edited Recipe
if request.method == 'POST' and form.is_valid():
form.save()
return HttpResponseRedirect('/recipes/') #TODO: write a url + add it to urls .. this is temp
return render_to_response('adding_recipe_form.html',{'form':form})
then should I create 1 modelform for the 2 models related to each other as I did ? 那我应该像我一样为彼此相关的2个模型创建1个模型表单吗? or a modelform for each model ?
或每个模型的模型形式? If I do one, how I'm going to exclude fields from the other model ?
如果我做一个,我将如何从另一个模型中排除字段? I'm a bit confused.
我有点困惑。
1.Should I create 1 model form for the 2 models related to each other as I did ? 1.是否应该像我一样为彼此相关的两个模型创建一个模型表格? No, you cant.
不,你不能。 Django use's this list to do a model field to form field mapping.
Django使用此列表执行模型字段以形成字段映射。 Related fields are shown as select/drop down box.
相关字段显示为选择/下拉框。 These select/drop down box are populated with existing instances of the related field.
这些选择/下拉框填充有相关字段的现有实例。
2.Model form for each model? 2.每个模型的模型形式? Its better to create a model form for each model.
最好为每个模型创建一个模型表单。
3.If I do one, how I'm going to exclude fields from the other model ? 3.如果我做一个,我该如何从另一个模型中排除字段? If you create a model form for each model then you can use
exclude
in their individual model forms. 如果为每个模型创建一个模型表单,则可以在其各个模型表单中使用
exclude
。
say for eg: 举例来说:
class CategoryForm(ModelForm):
class Meta:
model = Category
exclude = ('slug ')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.