이 항목에 대한 많은 질문이 있지만이 문제를 해결할 수있는 항목을 찾을 수 없었습니다 . 나는 Ingredient
및 Instruction
에 대한 외래 키 세트가있는 Recipe
객체를 생성하기 위해 뷰를 만들려고합니다. 제출을 시도하면 formsets에서 오류 'recipe': [u'The inline foreign key did not match the parent instance primary key.']
이 표시됩니다.Django 인라인 formset : 인라인 외래 키가 부모 인스턴스 기본 키와 일치하지 않습니다.
def recipe_add(request):
profile = profile_from_request(request)
if request.method == 'POST':
recipe_form = RecipeForm(request.POST)
if recipe_form.is_valid():
recipe = recipe_form.save(commit=False)
recipe.user = profile_from_request(request)
ingredient_form = IngredientFormSet(request.POST, prefix='ingredient', instance=recipe)
instruction_form = InstructionFormSet(request.POST, prefix='instruction', instance=recipe)
if ingredient_form.is_valid() and instruction_form.is_valid():
recipe = recipe.save()
ingredient_form.save()
instruction_form.save()
messages.success(request, _("Recipe added."))
return HttpResponseRedirect(reverse("recipes:recipe_list"))
else: # GET
recipe_form = RecipeForm()
ingredient_form = IngredientFormSet(prefix='ingredient', instance=Recipe())
instruction_form = InstructionFormSet(prefix='instruction', instance=Recipe())
return render(
request, 'recipes/recipe_add.html',
{
'profile': profile,
'recipe_form': recipe_form,
'ingredient_form': ingredient_form,
'instruction_form': instruction_form,
}
)
문제가 GET
또는 POST
방법에 formsets를 만들 수 오면 나는 확실하지 않다 : 여기 내 전체보기입니다. 나는 두 가지 모두에서 instance
인수를 사용하는 것을 시도했지만 아무런 효과가 없다.