Bienvenue sur IndexError.

Ici vous pouvez poser des questions sur Python et le Framework Django.

Consultez la FAQ pour améliorer vos chances d'avoir des réponses à vos questions.

Update avec inlineformset

+1 vote

J'ai un formulaire avec 3 formulaires différents créés par 3 classes différentes.

J'ai réussi à créer les vues pour créer une entrée mais j'ai un problème avec la vue qui me permettrait de modifier les entrées.

J'obtiens l'erreur suivante lorsque je valide le formulaire:

la clé étrangère en ligne ne correspond pas à la clé primaire de
l'instance parente.

Je pense comprendre l'erreur mais je ne vois pas comment faire correspondre les clés des entrées à vrai dire.

Voici le code de ma classe:

class CoachUpdateView(WithUserMixin, UpdateView):

model = User
form_class = UserForm
slug_field = 'member__user__username'
template_name = "staffManagement/coach_create.html"
success_url = "/staff/"

def get(self, request, *args, **kwargs):

    self.object = self.get_object()
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    coach_item_form = CoachFormSet(instance = self.get_object().member)
    member_item_form = MemberFormSet(instance = self.get_object())
    return self.render_to_response(self.get_context_data(form = form,
        coach_item_form = coach_item_form, member_item_form = member_item_form))

def post(self, request, *args, **kwargs):

    self.object = None
    form_class = self.get_form_class()

    form = self.get_form(form_class)

    coach_item_form = CoachFormSet(self.request.POST, instance = self.object)

    member_item_form = MemberFormSet(self.request.POST, instance = self.object)

    if form.is_valid() and coach_item_form.is_valid() and member_item_form.is_valid():
        return self.form_valid(form, coach_item_form, member_item_form)
    else:
        return self.form_invalid(form, coach_item_form, member_item_form)

def form_valid(self, form, coach_item_form, member_item_form):
    self.object = form.save()

    member_item_form.instance = self.object
    member_created = member_item_form.save()

    coach_item_form.instance = member_created[0]
    coach_item_form.save()

    return HttpResponseRedirect(self.get_success_url())

def form_invalid(self, form, coach_item_form, member_item_form):
    return self.render_to_response(self.get_context_data(form = form,
        coach_item_form = coach_item_form, member_item_form =
        member_item_form))
demandé 6-Mai-2016 par Echel8n (246 points)
reclassée 6-Mai-2016 par foxmask

je n'ai jamais eu besoin de toucher à get / post / form_invalid, j'ai juste touché à get_context_data et form_valid comme ceci

https://github.com/foxmask/dj-diabetes/blob/master/dj_diabetes/views/exams.py

1 Réponse

+1 vote
 
Meilleure réponse

Merci à foxmask.

Voici la solution à mon problème:

class CoachUpdateView(WithUserMixin, UpdateView):
"""Update a coach
"""

model = User
form_class = UserForm
slug_field = 'member__user__username'
template_name = "staffManagement/coach_create.html"
success_url = "/staff/"

def get_context_data(self, **kw):
    context = super(CoachUpdateView, self).get_context_data(**kw)
    if self.request.POST:
        context['coach_item_form'] = CoachFormSet(self.request.POST)
        context['member_item_form'] = MemberFormSet(self.request.POST)
    else:
        context['coach_item_form'] = CoachFormSet(instance=self.object.member)
        context['member_item_form'] = MemberFormSet(instance=self.object)
    return context

def form_valid(self, form):
    coach_formset = CoachFormSet((self.request.POST or None), instance=self.object.member)
    member_formset = MemberFormSet((self.request.POST or None), instance=self.object)

    if coach_formset.is_valid() and member_formset.is_valid():
        self.object = form.save()
        coach_formset.instance = self.object
        member_formset.instance = self.object
        coach_formset.save()
        member_formset.save()

    return HttpResponseRedirect(self.get_success_url())
répondu 6-Mai-2016 par Echel8n (246 points)
sélectionné 6-Mai-2016 par Echel8n

si ca marche, valider votre reponse pour qu'on sache qu'il n'y a plus besoin d'aide :)

...