Alejandro Veintimilla
Alejandro Veintimilla

Reputation: 11523

Django Change inherited form meta class

I want to edit the Meta Class of a form that inherits its data from a ModelForm. All I want is to add one field, I don't want to repeat all the form.

# NuevaBibliotecaCompartida is a ModelForm

class EditarBibliotecaCompartida(NuevaBibliotecaCompartida):

    class Meta:
        fields = ('nombre', 'direccion', 'imagen', 'punto_google_maps')

I get the error ModelForm has no model class specified, of course, because I am overriding the Meta class when I add a field. How can I solve this?

Upvotes: 7

Views: 3765

Answers (2)

David
David

Reputation: 502

if you do not want to type the fields of the parent form class you can also get them with the ParentClass.Meta.fields:

class Meta(NuevaBiblioteca.Meta):
    fields = NuevaBiblioteca.Meta.fields + ('YourAddedFormField',)

Upvotes: 14

solarissmoke
solarissmoke

Reputation: 31404

You need to explicitly subclass the parent's Meta class:

class Meta(NuevaBibliotecaCompartida.Meta):
    # `model` will now be inherited
    fields = ('nombre', 'direccion', 'imagen', 'punto_google_maps')

Upvotes: 12

Related Questions