2

I'm creating the "Order" form for the Parts.

My AddOrderForm has Order Model fields (like quantity) but at the same time some Part-related info from Part Model (like name_in_english) and PartNumber Model (like number) and some other fields...

First I did Separate ModelForm for each one and them combined them like this:

forms.py

class PartNumberForAddOrderForm(forms.ModelForm):

    class Meta:
        model = PartNumber
        fields = ('number',)

class PartForAddOrderForm(forms.ModelForm):

    class Meta:
        model = Part
        fields = ('manufacturer','name_in_english')

class AddOrderForm(forms.ModelForm):

    class Meta:
        model = Order
        fields = ('quantity',)

in template:

<form class="form-inline well" id="add_order">
    <div class="row-fluid">
        <div class="span12">

        {% crispy form_part_number %}
        {% crispy form_part %}
        {% crispy form_order %}
        
        <input type="submit" class="btn btn-primary pull-right" value="{% trans 'Add' %}"/>
        </div>
    </div>  
</form>

With this view became: form_part_number.is_valid() and form_part.is_valid() and form_order.is_valid():

All this looks SUPER ugly and just wrong...

So I created the other way:

class PartForm(forms.ModelForm):

    class Meta:
        model = Part
        fields = ('manufacturer','name_in_english')

class PartNumberForm(forms.ModelForm):

    class Meta:
        model = PartNumber
        fields = ('number',)

class AddOrderForm(forms.ModelForm):
    manufacturer = PartForm.base_fields['manufacturer']
    name_in_english = PartForm.base_fields['name_in_english']
    number = PartNumber.base_fields['number']

    class Meta:
        model = Order
        fields = ('manufacturer','number','quantity','name_in_english')
    

QUESTIONS: Is this Form.base_fields[''] legal way? I assume the .save() will not work, but .is_valid() should be fine, right? Is there any better way to do it?

I could fine anything helpful anywhere so this is something I came up with myself. and that worries me.

Thanks

1 Answer 1

1

The is_valid() call will probably validate the individual fields correctly, but it might ignore other model validation, for example a unique_together statement or a custom clean method on one of the other models.

I don't think that checking is_valid() for multiple forms is that ugly. I've used it before, and immediately understand what you're doing. However, I've never seen anyone define a form by grabbing a fields from another forms base_fields before. That doesn't mean it won't work, but it is unconventional.

If you're really concerned about it being ugly, you could do something like the following:

my_forms = [form_part_number, form_part, form_order]
if all(f.is_valid() for f in my_forms):
    ...
Sign up to request clarification or add additional context in comments.

2 Comments

@Alasdair, in my actual case I had to use 5 "forms" in one form - 2 of them due to the order of the fields that I wanted. But what makes me sick to my stomach is the fact that I'm using multiple "forms" in one actual form. I'm new to Django and trying to figure things out.
I'm still sticking to base_fields... so far so good. Thank you, Alasdair

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.