[django]AttributeError: ’str’ object has no attribute ‘_meta’
Posted | archive
A very common error among django learner is like this:
From django.db import models
From django.contrib.auth.models import User
Create your models here.
class DLUser(models.Model):
xxxx = models.xxxxField()
user = models.OneToOneField('CustomUser', User)
...
When execute python manage.py validate you’ll get
Traceback (most recent call last):
File "manage.py", line 13, in
execute_manager(settings)
File "C:\Python25\Lib\site-packages\django\core\management.py", line 1672, in
execute_manager
execute_from_command_line(action_mapping, argv)
File "C:\Python25\Lib\site-packages\django\core\management.py", line 1569, in
execute_from_command_line
action_mapping[action]()
File “C:\Python25\Lib\site-packages\django\core\management.py”, line 1152, in
validate
num_errors = get_validation_errors(outfile)
File “C:\Python25\Lib\site-packages\django\core\management.py”, line 1018, in
get_validation_errors
for r in rel_opts.get_all_related_objects():
File “C:\Python25\Lib\site-packages\django\db\models\options.py”, line 125, in
get_all_related_objects
if f.rel and self == f.rel.to._meta:
AttributeError: ’str’ object has no attribute ‘_meta’
The error message is misleading. Infact you just made a mistake here
user = models.OneToOneField('CustomUser', User)
Correct that to
user = models.OneToOneField(User, 'CustomUser')
Or, according to official defination of verbose-field-names, ForeignKey, ManyToManyField and OneToOneField require the first argument to be a model class, so use the verbose_name keyword argument:
user = models.OneToOneField(User, verbose_name='CustomUser')
And the problem is solved.
Comments