7

我尝试设置一个简单的 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 的常用表同步)

4

1 回答 1

15

问题是

email = models.EmailField

将其更改为

email = models.EmailField()

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

于 2012-07-23T16:15:06.193 回答