Django ModelForm has no model class specified
Question
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.
Solution
It should be model
instead of Model
(and without the trailing `, but I guess that's a typo):
class PickForm(ModelForm):
class Meta:
model = Car
OTHER TIPS
Just do this method your page will run:
class PickForm(ModelForm):
class Meta:
model = Car
fields = "__all__"
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.
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.