根据 Extending the existing User model¶
These profile models are not special in any way - they are just Django models that happen to have a one-to-one link with a user model. As such, they aren’t auto created when a user is created, but a
django.db.models.signals.post_save
could be used to create or update related models as appropriate.
但是我如何使用 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' is an invalid keyword argument for this function
在使用Employee.objects.create(username='username1', password='passwd')
时,出现了类似的错误:
TypeError: 'username' is an invalid keyword argument for this function
这是完整的堆栈跟踪:
(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
>>>
最佳答案
您不能以这种方式使用信号!
@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 中的前三个参数将是 raw
、using
和 update_fields
。它们并不完全符合您的模型要求。
第二个问题是,当您收听信号时,过程并不总是在您的控制之下。无法保证接收方将获得您想要的所有字段的数据。
不过,最重要的问题是您对扩展 User
模型的方式有误解。解决方案在我对您上一个问题的回答中给出。
关于python - 在创建实例时创建配置文件模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43949270/