Django receiver - DoesNotExist: The WidgetList matching query does not exist

advertisements

I'm currently using @receiver to add a token to new users like so -

@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)

What I also want to be able to do is create a default WidgetList based on this model:

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']

I tried making another reciever -

@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)

EDIT - on the initial python manage.py migrate I get the following traceback:

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

this is the second traceback if I try to rerun migrate:

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.


Comparing your traceback and your code, you can find that error happens here:

WidgetList.objects.create(user=instance)

Your model WidgetList doesn't even have user field, so it's pretty obvious that you cannot do that. Maybe you were copying & paste the code that caused an error.