[Django]-How can I make a Django model form with a field name in the form different from the model field name?

5👍

Your comment reveals what you actually need to do – this is why you should always describe your actual problem, not your proposed solution. Naturally, there is a proper way to deal with two identical forms on the same page in Django – use the prefix parameter when instantiating the field.

form1 = MyForm(prefix='form1')
form2 = MyForm(prefix='form2')

Now when you output form1 and form2, all the fields will automatically get the relevant prefix, so they are properly separated.

1👍

I’m not sure what you mean by “the name of the field in the form”. Do you mean the label? Or the id? Or something else? Configuring the label is pretty easy:

class ContentForm(forms.ModelForm):
    category = forms.ModelChoice(queryset=Category.objects.all(), label='f_category')
    class Meta:
        model=Content
        fields = ('title', 'category', )

Leave a comment