ModelForm has no model class specified.

808 views
Skip to first unread message

Matthew Pava

unread,
Apr 4, 2017, 4:04:52 PM4/4/17
to django...@googlegroups.com

I have a form similar to that declared below:

 

class LineItemForm(Mixin, forms.ModelForm):

cost = forms.FloatField()

field_order = [‘part’, ‘cost’]

 

class Meta:

      model = LineItem

      fields = [‘part’]

 

And then I have another form that inherits from that:

 

class InventoryTransactionForm(LineItemForm):

      class Meta:

            fields = [‘part’, ‘document’]

            exclude = []

 

 

I have two views that use the InventoryTransactionForm.  View A uses it in a formset like so:

 

InventoryFormset = modelformset_factory(LineItem, InventoryTransactionForm, formset=InventoryTransactionFormSet)

inventory_formset = InventoryFormset(request.POST or None, initial=[{"part": default_part}])

 

View B uses just the form:

 

form = InventoryTransactionForm(

        request.POST or None, instance=inventorytransaction.transaction.lineitem, prefix="inventory", create_mode=False

    )

 

When I load View B, I get this exception:

ModelForm has no model class specified.

 

Everything works fine in View A.

Any ideas, thoughts, or suggestions?

Thank you,

Matthew

 

Daniel Roseman

unread,
Apr 5, 2017, 9:36:21 AM4/5/17
to Django users, Matthe...@iss.com
You've overridden the inner Meta class completely in InventoryTransactionForm, and don't supply a model. Either inherit that Meta from LineItemForm.Meta or, much more simply, just add `model = LineItem`.
--
DR.
Reply all
Reply to author
Forward
0 new messages