Django ModelForm has no model class specified
Asked Answered
M

10

41

I am trying to use ModelForm:

from django.db import models
from django.forms import ModelForm

class Car(models.Model):
    carnumber = models.CharField(max_length=5)

    def __unicode__(self):
        return self.carnumber

class PickForm(ModelForm):
    class Meta:
        Model = Car

I have checked this and I cannot find my error. When I call the view in a browser, it gives me the following error:

ModelForm has no model class specified

I have tested the view that calls the model with simple "foo bar" code at the same URL, but when I try this code, I get the class error above.

Modulate answered 25/1, 2012 at 23:35 Comment(0)
T
71

It should be model instead of Model:

class PickForm(ModelForm):
    class Meta:
        model = Car
Taite answered 25/1, 2012 at 23:45 Comment(0)
M
12

my error was because I wrote meta instead of Meta.

Megagamete answered 28/2, 2020 at 7:28 Comment(1)
Yeah sure class Meta not metaJumpoff
S
8

Just do this method your page will run:

class PickForm(ModelForm):
  class Meta:
    model = Car
    fields = "__all__"
Silda answered 27/11, 2017 at 16:34 Comment(0)
C
5

In my case, the error occurs because I wrote class META instead, it should be class Meta

Caracul answered 7/1, 2020 at 15:25 Comment(1)
in my case this happendCharis
O
3

In my case the error was due to me wirting :

models = User


instead of :

model = User
Odetteodeum answered 19/3, 2020 at 8:33 Comment(0)
C
1
class PickForm(ModelForm):
    class Meta:
        Model = Car`

Here the Model is case sensitive, so use model instead of Model.

so the code becomes

class PickForm(ModelForm):
    class Meta:
        model = Car
Cellulitis answered 31/8, 2021 at 9:37 Comment(0)
S
0

If this is a copy and past, you have a typo. I would highly recommend you use an IDE or something with error checking. Eclipse is what I use. It will save you a ton of time from little annoyances like this.

class PickForm(ModelForm):
    class Meta:
        Model = Car`

Your typo is right on the end of Car. The little apostrophe thing.

Semiotics answered 25/1, 2012 at 23:47 Comment(1)
I am using geany, I will check out Eclipse.Modulate
M
0

see your code in your defined form code area.there may be error either with =,: and meta,Meta .so please look carefully if case sensitive error or any signs over there

Muire answered 5/9, 2020 at 15:48 Comment(0)
S
0

I got this error just by writing model: Comment instead of model = Comment 🤦‍♂️

Solangesolano answered 26/9, 2022 at 13:51 Comment(0)
F
-1

You missed the basic step of registering your model to the admin. Please do that and that should work for you.

In the admin.py file of your app add these lines:

from yourapp.models import yourmodel
admin.site.register(yourmodel)

Here yourapp and yourmodel needs to be replaced with the correct names for your app and model.

Fallow answered 7/12, 2017 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.