[Django]-AttributeError: 'module' object has no attribute 'model'

77👍

It’s called models.Model and not models.model (case sensitive). Fix your Poll model like this –

class Poll(models.Model):
    question = models.CharField(max_length=200) 
    pub_date = models.DateTimeField('date published')

8👍

I also got the same error but I noticed that I had typed in Foreign*k*ey and not Foreign*K*ey,(capital K) if there is a newbie out there, check out spelling and caps.

👤sam

3👍

Searching

AttributeError: ‘module’ object has no attribute ‘BinaryField’

landed me here.

The above answers did not solve the problem, so I’m posting my answer.

BinaryField was added since Django 1.6. If you have an older version, it will give you the above error.

You may want to check the spelling of the attribute first, as suggested in the above answers, and then check to make sure the module in the Django version indeed has the attribute.

👤azalea

2👍

As the error message says in the last line: the module models in the file c:\projects\mysite..\mysite\polls\models.py contains no class model. This error occurs in the definition of the Poll class:

class Poll(models.model):

Either the class model is misspelled in the definition of the class Poll or it is misspelled in the module models. Another possibility is that it is completely missing from the module models. Maybe it is in another module or it is not yet implemented in models.

2👍

I realized that by looking at the stack trace it was trying to load my own script in place of another module called the same way,i.e., my script was called random.py and when a module i used was trying to import the “random” package, it was loading my script causing a circular reference and so i renamed it and deleted a .pyc file it had created from the working folder and things worked just fine.

2👍

In class poll, you inherited your class from models.model but there’s no module in models called that name.

Because Python is case sensitive, you need to use the capital Model instead of model.

class poll(models.Model):
...
👤Sonic

0👍

Change this
class Poll(models.model):

to this

class Poll(models.Model):

problem was models.model —> models.Model

0👍

I had a similar issue. I have given from django.db import models in my admin.py file. After modifying it to from MyAppName import models, my issue is resolved. (Though I don’t understand y I am supposed to import models from my app and not django.db) Also, one more thing just verify how you have defined your primary key. Make sure to add this DEFAULT_AUTO_FIELD = ‘django.db.models.BigAutoField’ in your setting.py file if you haven’t exclusively defined your primary key.

Leave a comment