django receiver - DoesNotExist:WidgetList匹配查询不存在

时间:2016-01-31 19:48:21

标签: django django-models

我目前正在使用@receiver向新用户添加令牌 -

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if created:
        Token.objects.create(user=instance)

我还希望能够创建基于此模型的默认WidgetList:

class WidgetList(MPTTModel):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=1024)
    owner = models.ForeignKey('MyAppUser')
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)

    def __str__(self):
        return self.name

    class MPTTMEta:
        order_insertion_by = ['name']

我尝试制作另一个接收器 -

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_root_list(sender, instance=None, created=False, **kwargs):
    if created:
        WidgetList.objects.create(user=instance)

编辑 - 在最初的python manage.py migrate我得到以下追溯:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
    utility.execute()
  File "/opt/myproject_app/lib/python2.7/site-packages/django/core/management/__init__.py", line 343, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 226, in handle
    emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/core/management/sql.py", line 280, in emit_post_migrate_signal
    using=db)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 201, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/opt/myproject_app/lib/python2.7/site-packages/guardian/management/__init__.py", line 39, in create_anonymous_user
    user.save()
  File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/base.py", line 734, in save
    force_update=force_update, update_fields=update_fields)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/base.py", line 771, in save_base
    update_fields=update_fields, raw=raw, using=using)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 201, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/opt/myproject/core/models.py", line 18, in create_root_list
    WidgetList.objects.create(user=instance)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/query.py", line 346, in create
    obj = self.model(**kwargs)
  File "/opt/myproject_app/lib/python2.7/site-packages/mptt/models.py", line 393, in __init__
    super(MPTTModel, self).__init__(*args, **kwargs)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/base.py", line 480, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'user' is an invalid keyword argument for this function

如果我尝试重新运行迁移,这是第二次追溯:

Traceback (most recent call last):
  File "/opt/myproject_app/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/opt/myproject_app/lib/python2.7/site-packages/rest_framework/viewsets.py", line 87, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/myproject_app/lib/python2.7/site-packages/rest_framework_encrypted_lookup/views.py", line 41, in dispatch
    return super(EncryptedLookupGenericViewSet, self).dispatch(request, *args, **kwargs)
  File "/opt/myproject_app/lib/python2.7/site-packages/rest_framework/views.py", line 466, in dispatch
    response = self.handle_exception(exc)
  File "/opt/myproject_app/lib/python2.7/site-packages/rest_framework/views.py", line 463, in dispatch
    response = handler(request, *args, **kwargs)
  File "/opt/myproject_app/lib/python2.7/site-packages/rest_framework/mixins.py", line 21, in create
    self.perform_create(serializer)
  File "/opt/myproject_app/lib/python2.7/site-packages/rest_framework/mixins.py", line 26, in perform_create
    serializer.save()
  File "/opt/myproject_app/lib/python2.7/site-packages/rest_framework/serializers.py", line 191, in save
    self.instance = self.create(validated_data)
  File "/opt/myproject/api/serializers.py", line 106, in create
    validated_data['parent'] = WidgetList.objects.get(pk=user_parent_list)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/opt/myproject_app/lib/python2.7/site-packages/django/db/models/query.py", line 334, in get
    self.model._meta.object_name
DoesNotExist: WidgetList matching query does not exist.

2 个答案:

答案 0 :(得分:1)

比较您的追溯和您的代码,您可以在此处找到错误:

WidgetList.objects.create(user=instance)

您的模型WidgetList甚至没有user字段,因此很明显您无法做到这一点。也许你是在复制&amp;粘贴导致错误的代码。

答案 1 :(得分:1)

  1. 您的模型中没有用户。我认为你的意思是老板。
  2. 由于没有数据,您将收到外键ref错误。使用fixture加载一些父数据。这样可以解决这个问题。