0

添加用户配置文件(此处称为用户信息)后,我的 models.py 文件不会同步数据库。我明白了:TypeError: __init__() takes at least 2 arguments (1 given)上面提到的所有错误都提到了 models.py 文件或与之相关的一些文件。

模型.py 文件:

def create_user_info(sender, instance, created, **kwargs):
    if created:
        UserInfo.objects.create(user=instance)

post_save.connect(create_user_info, sender=User)

class Book(models.Model):
    title = models.CharField(max_length=50)
    author = models.CharField(max_length=50)
    def __unicode__(self):
        return self.title

class UserInfo(models.Model):
    user = models.OneToOneField(User)
    pen_name = models.CharField(max_length=30)
    activated = models.BooleanField()
    def __unicode__(self):
        return self.email + '-' + self.pen_name

在 settings.py 中进行身份验证:

AUTH_PROFILE_MODULE = 'Knights.UserInfo'

追溯:

Traceback (most recent call last):
  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 53, in <module>
    class Alert(models.Model):
  File "/Users/Mike/Desktop/Main/Django-Development/BBN/Knights/models.py", line 55, in Alert
    for_user = models.ForeignKey()
4

2 回答 2

3

您的错误不在于您发布的代码。您已经按照文档进行了操作,所以如果确实如此,我会感到惊讶。

相反,错误是在您的 Alert 类 (models.py:55) 中发现的。for_user = models.ForeignKey()必须是for_user = models.ForeignKey(User)or for_user = models.ForeignKey(UserInfo),这取决于您喜欢哪个。

您的错误代码试图实例化类的实例,该实例ForeignKey调用它的__init__()方法。该方法将声明类似

def __init__(self, other_model):

因此错误消息表明您缺少一个参数。self隐式传递,如果您有兴趣,请阅读 Python 类文档。

于 2012-08-03T19:04:28.053 回答
-2

试试这个

class UserInfo(models.Model):
    user = models.OneToOneField(User)
    pen_name = models.CharField(max_length=30)
    activated = models.BooleanField()
    def __unicode__(self):
        return self.email + '-' + self.pen_name

    def create_user_info(self, **kwargs):
         u = kwargs["instance"]
         created = kwargs["created"]
         if not created:
            UserInfo(user=u).save()

post_save.connect(UserInfo.create_user_info, sender=User)
于 2012-08-03T18:36:39.900 回答