Django:我的models.py文件似乎无法同步到数据库。

时间:2012-07-23 16:13:25

标签: python database django terminal syncdb

我尝试设置一个简单的models.py文件,作为我在线跟踪的本教程的一部分。当我尝试使用syncdb命令时,出现以下错误:

      File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/base.py", line 231, in execute
    self.validate()
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/base.py", line 266, in validate
    num_errors = get_validation_errors(s, app)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/validation.py", line 30, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/db/models/loading.py", line 158, in get_app_errors
    self._populate()
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/db/models/loading.py", line 64, in _populate
    self.load_app(app_name, True)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/db/models/loading.py", line 88, in load_app
    models = import_module('.models', app_name)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/Mike/Desktop/Main/Django-Development/BBN/Knights/models.py", line 3, in <module>
    class Users(models.Model):
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/db/models/base.py", line 99, in __new__
    new_class.add_to_class(obj_name, obj)
  File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/db/models/base.py", line 219, in add_to_class
    value.contribute_to_class(cls, name)
TypeError: Error when calling the metaclass bases
    unbound method contribute_to_class() must be called with EmailField instance as first argument (got ModelBase instance instead)

这是我的models.py文件:

from django.db import models

class Users(models.Model):
    pen_name = models.CharField(max_length=30)
    email = models.EmailField

class Works(models.Model):
    user = models.ForeignKey(Users)
    date_published = models.DateField()

class Reviews(models.Model):
    work = models.ForeignKey(Works)
    date_published = models.DateField()

class Works_Subscriptions(models.Model):
    user = models.ForeignKey(Users)
    to_work = models.ForeignKey(Works)

class User_Subscriptions(models.Model):
    user = models.ForeignKey(Users)
    to_user = models.ForeignKey(Users)

class Books(models.Model):
    title = models.CharField(max_length=50)
    author = models.CharField(max_length=50)

如果有任何帮助,我正在使用sqlite3,之前我在models.py文件中没有任何内容时工作(因此它只是将数据库与django的常用表同步)

1 个答案:

答案 0 :(得分:15)

问题是

email = models.EmailField

将其更改为

email = models.EmailField()

这是因为Django模型类的属性被django.db.models.Model的元类修改,而不是直接作为标准属性分配,因此它们可以透明地与数据库进行通信。它变得困惑,因为你试图给它EmailField类而不是该类的实例。