У меня есть вопрос по ModelForms в Django. Если я создаю форму из модели с использованием ModelForms, то как поля формы будут связаны с этими отношениями M2M? что я имею в виду, если у меня есть:
Models.py
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']
Forms.py
class RecipeForm(ModelForm):
class Meta:
model = Recipe
exclude = ('added_at','last_update','added_by')
Views.py
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})
тогда я должен создать 1 модель для двух моделей, связанных друг с другом, как я сделал? или модель для каждой модели? Если я сделаю один, как я собираюсь исключить поля из другой модели? Я немного запутался.