Hi
-- I don't know if this is a common question, but I couldn't find anything resembling my problem.
I have a page called "Case". On this page some basic information is added along with the creation of 1 or 2 clients (fixed) Client's are FK to the Case model. These are added using inlineformset factory.
On the subsequent page, and the one with the problem, I need to query my Client model to see how many clients are associated with the Case. That determines how many formsets I should show. ie. 1 for 1 client, or 2 for 2 clients.
I chose to use modelformset factory, instead of inline as I don't know how many clients exist.
Each formset is designed to associate family relatives to each client and are therefor FK'd to the Client. eg: Parent model contains FK to Client.
My very rudamentary code below achieves this, however my modelformset factories that I am using to generate the forms don't respect the "unique_together" constraint. I suspect this is because I am not passing an instance to the formset on post. But because for the sake of argument "Parent" instance may not exist and instead is being POST'd to create, I cannot specify an instance.
@login_required
def create_new_family(request, case_pk):
case = Case.objects.get(pk=case_pk)
clients = Client.objects.filter(case=case_pk)[:2]
formset = {}
for client in clients:
formset[client.pk] = {}
formset[client.pk] = {}
if request.method == 'POST':
for client in clients:
formset[client.pk]['parents'] = ParentsFormset(request.POST, prefix='parents_' + str(client.pk),)
formset[client.pk]['siblings'] = SiblingsFormset(request.POST, prefix='siblings_' + str(client.pk))
formset[client.pk]['children'] = ChildrenFormset(request.POST, prefix='children_' + str(client.pk))
formset[client.pk]['stepchildren'] = StepChildrenFormset(request.POST, prefix='stepchildren_' + str(client.pk))
err = 0
for client_id, fs_dict in formset.items():
if not all(form.is_valid() for fs_type, form in fs_dict.items()):
err = err + 1
if err == 0:
for client_id, fs_dict in formset.items(): # a is client_id ## b is formset dict
if all(form.is_valid() for fs_type, form in fs_dict.items()):
for idx, fs in fs_dict.items(): # at this point all fs are formset instances
if fs.is_valid():
instances = fs.save(commit=False)
for instance in instances:
instance.client = Client.objects.get(pk=client_id)
instance.save()
return HttpResponseRedirect(reverse('family-create', kwargs={'case_pk': case_pk}))
else:
for client in clients:
formset[client.pk]['parents'] = ParentsFormset(prefix='parents_' + str(client.pk),
queryset=Parent.objects.filter(client=client))
formset[client.pk]['siblings'] = SiblingsFormset(prefix='siblings_' + str(client.pk),
queryset=Sibling.objects.filter(client=client))
formset[client.pk]['children'] = ChildrenFormset(prefix='children_' + str(client.pk),
queryset=Children.objects.filter(client=client))
formset[client.pk]['stepchildren'] = StepChildrenFormset(prefix='stepchildren_' + str(client.pk),
queryset=StepChildren.objects.filter(client=client))
return render(request, 'clients/client_form.html', {
'formsets': formset,
'clients': clients,
'case': case,
'breadcrumbs': 'Family & Dependants'
})
I appreciate this might be difficult to comprehend, as it is for me to explain.
Any help would be much appreciated.
Thank you
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/17499d8a-c18f-4a68-9173-216a08649871%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment