Django:我的模型。py文件似乎无法与数据库同步。

时间:2021-12-22 02:46:29

I tried setting up a simple models.py file as part of this tutorial that I was following online. When I tried the syncdb command, I got the following errors:

我试着建立一个简单的模型。作为本教程的一部分,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)

This is my models.py file:

这是我的模型。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)

If it's any help, I'm using sqlite3 and it worked before when I had nothing in the models.py file (so it was just syncing the database with django's usual tables)

如果有什么帮助的话,我使用sqlite3,它以前在模型中什么都没有的时候是有用的。py文件(因此它只是将数据库与django的常规表同步)

1 个解决方案

#1


15  

The problem is

现在的问题是

email = models.EmailField

Change it to

将其更改为

email = models.EmailField()

This is because attributes of Django model classes are modified by the metaclass of django.db.models.Model rather than assigned directly as standard attributes, so that they can transparently talk to the database and all that. It's getting confused because you're trying to give it the EmailField class rather than an instance of that class.

这是因为Django模型类的属性是由Django .db.models的元类修改的。模型而不是直接指定为标准属性,这样它们就可以透明地与数据库和所有这些属性对话。它会变得混乱,因为你想给它一个EmailField类而不是那个类的一个实例。

#1


15  

The problem is

现在的问题是

email = models.EmailField

Change it to

将其更改为

email = models.EmailField()

This is because attributes of Django model classes are modified by the metaclass of django.db.models.Model rather than assigned directly as standard attributes, so that they can transparently talk to the database and all that. It's getting confused because you're trying to give it the EmailField class rather than an instance of that class.

这是因为Django模型类的属性是由Django .db.models的元类修改的。模型而不是直接指定为标准属性,这样它们就可以透明地与数据库和所有这些属性对话。它会变得混乱,因为你想给它一个EmailField类而不是那个类的一个实例。