Luis Bermúdez
Luis Bermúdez

Reputation: 654

error ModelForm has no model class specified in django

i am working with django and i have the form:

from django import forms
from clientes.administrativo.models import Customer

class CustomerForm(forms.ModelForm):
    """Formulario para el catálogo de clientes"""
    model = Customer
    class Meta:
        fields = ['code_customers', 'trade_name', 'legal_name','name_owner',
        'name_manager','address','email','phone','city','post_code','customers_type',
        'locked','special_credit','tax_exempt','distributor','customers_subgroup',
        'final_consumer','retention_agent','nit','contac']

    def clean(self):
        cleaned_data = super().clean()
     


    def save(self, commit=True):
        model = super().save(commit=False)
        if commit:
            model.save()
        return model

    

But when i try to do this save() with this method:

@api_view(['POST', 'PUT', 'GET'])
@permission_classes([LoginRequired])
@renderer_classes([JSONRenderer])
def register_customer(request, format=None):
    customerFM = CustomerForm(request.data)
    if customerFM.is_valid():
        customerFM.save()
        return {'success': True,'message': ''}
    return {'success': False,'message': 'Hubo un error, por favor intente nuevamente..'}

I get the error ModelForm has no model class specified...

Any help with this? Thank you!...

Upvotes: 0

Views: 45

Answers (2)

crimsonpython24
crimsonpython24

Reputation: 2383

The model=Customer should be inside the Meta class.

Upvotes: 2

Arjun Shahi
Arjun Shahi

Reputation: 7330

You need to specify the model class inside the Meta:

class Meta:
  model = Customer
  fields =[..]

Upvotes: 2

Related Questions