2

I've run into a problem with using two models for my form in Django. I have two models, Animal and Family, both with a "name" field.

I use the two modelforms on my template for my form, and when it's submitted, the POST data for 'name' only returns one value.

Short of renaming the 'name' field in my models, is there any way around this?

Thanks for your help. This is my first post here.

2 Answers 2

3

You could make use of the prefix-argument when initializing the modelforms;

animal_form = AnimalForm(request.POST or None, prefix="animal")
family_form = FamilyForm(request.POST or None, prefix="family")

Which will output something like;

<input id="id_animal-name" type="text" />
<input id="id_family-name" type="text" />
Sign up to request clarification or add additional context in comments.

Comments

0

You can get the different POST values that are under the same name with request.POST.getlist.

However, the correct value will then depend on the position of the input field in the form (I guess) and this really can't be a good idea. Change the name of the field, not in the model, but in your form class (I hope you use one).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.