2014-09-21 1 views
2

이 항목에 대한 많은 질문이 있지만이 문제를 해결할 수있는 항목을 찾을 수 없었습니다 . 나는 IngredientInstruction에 대한 외래 키 세트가있는 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 인수를 사용하는 것을 시도했지만 아무런 효과가 없다.

답변

1

내 서식 파일에 내 formset에 대한 관리 양식을 추가하는 것을 잊었 기 때문에 비슷한 문제가있었습니다.

{{ ingredient.management_form }} 

{{ instruction.management_form }} 

빠른 방법 브라우저에서 양식 코드를 검사하고이 관리 양식으로 렌더링되는 숨겨진 필드를 찾는 것입니다 확인 : 템플릿에서

, 당신은 이런 식으로 뭔가가 있어야합니다.

그런 다음 해당 양식 세트의 외래 키 값이 올바른 방향으로 사용자를 안내 할 수있는 레서피 기본 키와 일치하는지 확인할 수 있습니다.

관련 문제