在实例创建上创建配置文件模型

时间:2017-05-13 04:44:49

标签: python django database authentication signals

根据Extending the existing User model¶

  

这些配置文件模型在任何方面都不是特别的 - 它们只是Django   恰好与用户模型具有一对一链接的模型。如   这样,它们不是在创建用户时自动创建的,而是a   django.db.models.signals.post_save可用于创建或更新   相关模型酌情。

但是如何使用post_save将必要的参数传递给MyCustomeModel.objects.create?以官方文件为例:

  

models.py

from django.contrib.auth.models import User

class Employee(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    department = models.CharField(max_length=100)

from . import signals
  

signals.py

from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

from .models import Employee


@receiver(post_save, sender=User)
def create_fellow_on_user_create(sender, instance, created, **kwargs):
    if created:
        Employee.objects.create(user=instance, **kwargs)

当我尝试按User.objects.create(username='username1', password='passwd')

创建用户时发生错误
  

TypeError:'signal'是此函数的无效关键字参数

使用Employee.objects.create(username='username1', password='passwd')时,发生了类似的错误:

  

TypeError:'username'是此函数的无效关键字参数

这是完整的堆栈跟踪:

(django_tutorial) sunqingyaos-MacBook-Air:fellow_go sunqingyao$ python manage.py shellPython 3.6.1 (default, Mar 23 2017, 16:49:06) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> from pickup.models import Employee
>>> User.objects.create(username='username1', password='passwd')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/query.py", line 394, in create
    obj.save(force_insert=True, using=self.db)
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 80, in save
    super(AbstractBaseUser, self).save(*args, **kwargs)
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/base.py", line 806, in save
    force_update=force_update, update_fields=update_fields)
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/base.py", line 846, in save_base
    update_fields=update_fields, raw=raw, using=using,
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 193, in send
    for receiver in self._live_receivers(sender)
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 193, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "/Users/sunqingyao/PycharmProjects/fellow_go/pickup/signals.py", line 12, in create_fellow_on_user_create
    Fellow.objects.create(user=instance, **kwargs)
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/query.py", line 392, in create
    obj = self.model(**kwargs)
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/base.py", line 571, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'signal' is an invalid keyword argument for this function
>>> Employee.objects.create(username='username1', password='passwd')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/query.py", line 392, in create
    obj = self.model(**kwargs)
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/base.py", line 571, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'username' is an invalid keyword argument for this function
>>> 

1 个答案:

答案 0 :(得分:1)

您无法以这种方式使用信号!

@receiver(post_save, sender=User)
def create_fellow_on_user_create(sender, instance, created, **kwargs):
    if created:
        Employee.objects.create(user=instance, **kwargs)

这里的第一个问题是,根据method signature,** kwargs中的前三个参数将是rawusingupdate_fields。它们并不完全符合您的模型所需。

第二个问题是,当您收听信号时,该过程并不总是在您的控制之下。无法确保接收方能够获取所需的所有字段的数据。

但最重要的问题是,您对User模型的扩展方式存在误解。在我对你上一个问题的回答中给出了解决方法。