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.
There is no user in your model. Which I see you mean owner.
You will get foreign key ref errors as there is not data. Load some parent data using fixture. That would solve this issue.
Related
Overriding save method with super returns unique contraint error when creating object. How to solve it?
Adding context to the code. I need to create an object1 and I need to assign some value dependent on a related foreign object. I need to assign the value after creating an instance of object1, otherwise an error pops up that the object does not exist and there is no relationship.
def save(self, *args, **kwargs):
if self.pk is None:
super(IntoDocumentProduct, self).save(*args, **kwargs)
# some logic
# more logic
super(IntoDocumentProduct, self).save(*args, **kwargs)
self.full_clean()
I am also posting the error that shows up when I test the api in insomnia.
IntegrityError at /api/wms/intodocuments/products/create/
duplicate key value violates unique constraint "wms_dokumentprzyjeciaprodukt_pkey"
DETAIL: Key (id)=(151) already exists.
Below is the error that appears in the console. It directs specifically to the save() method in the model. I don't know what is wrong with it. After all, I can't use self.save(), because there will be a recursive loop.
Traceback (most recent call last):
File "W:\projects\foodgast\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\django\views\generic\base.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "W:\projects\foodgast\venv\Lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "W:\projects\foodgast\venv\Lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Sebastian\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 81, in inner
return func(*args, **kwds)
^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\src\wms\api\views.py", line 183, in post
serializer.save()
File "W:\projects\foodgast\venv\Lib\site-packages\rest_framework\serializers.py", line 212, in save
self.instance = self.create(validated_data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\rest_framework\serializers.py", line 962, in create
instance = ModelClass._default_manager.create(**validated_data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\models\query.py", line 671, in create
obj.save(force_insert=True, using=self.db)
File "W:\projects\foodgast\src\wms\models.py", line 621, in save
super(IntoDocumentProduct, self).save(*args, **kwargs)
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\models\base.py", line 812, in save
self.save_base(
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\models\base.py", line 863, in save_base
updated = self._save_table(
^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\models\base.py", line 1006, in _save_table
results = self._do_insert(
^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\models\base.py", line 1047, in _do_insert
return manager._insert(
^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\models\query.py", line 1791, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\models\sql\compiler.py", line 1660, in execute_sql
cursor.execute(sql, params)
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\backends\utils.py", line 103, in execute
return super().execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\backends\utils.py", line 84, in _execute
with self.db.wrap_database_errors:
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "W:\projects\foodgast\venv\Lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.IntegrityError: duplicate key value violates unique constraint "wms_dokumentprzyjeciaprodukt_pkey"
DETAIL: Key (id)=(150) already exists.
I'm like 90% sure you have to define your super properly. I tried what you have and didn't get a compile error, but it's definitely not best practices. Particularly, if you're not defining a return within your if statement. That would cause super to be called twice which could be throwing the exception due to the save function running twice. I typically set it right after my function declaration (my prefered method) or as my last line.
def save(self, *args, **kwargs):
super(IntoDocumentProduct, self).save(*args, **kwargs)
# your overwrite code
or
def save(self, *args, **kwargs):
# your overwrite code
super(IntoDocumentProduct, self).save(*args, **kwargs)
models.py
class CustomUser(AbstractUser):
username = PhoneNumberField(unique=True)
payloads:
{
"username": "+8801700000000",
"password1": "demo",
"password2": "demo",
"email": "demo#demo.com",
}
response:
Internal Server Error: /api/rest-auth/registration/
Traceback (most recent call last):
File "venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "venv\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "venv\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "venv\lib\site-packages\django\views\decorators\debug.py", line 89, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "venv\lib\site-packages\rest_auth\registration\views.py", line 46, in dispatch
return super(RegisterView, self).dispatch(*args, **kwargs)
File "venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "venv\lib\site-packages\rest_framework\generics.py", line 190, in post
return self.create(request, *args, **kwargs)
File "venv\lib\site-packages\rest_auth\registration\views.py", line 65, in create
user = self.perform_create(serializer)
File "venv\lib\site-packages\rest_auth\registration\views.py", line 73, in perform_create
user = serializer.save(self.request)
File "venv\lib\site-packages\rest_auth\registration\serializers.py", line 210, in save
adapter.save_user(request, user, self)
File "venv\lib\site-packages\allauth\account\adapter.py", line 242, in save_user
self.populate_username(request, user)
File "venv\lib\site-packages\allauth\account\adapter.py", line 209, in populate_username
user_username(
File "venv\lib\site-packages\allauth\account\utils.py", line 120, in user_username
return user_field(user, app_settings.USER_MODEL_USERNAME_FIELD, *args)
File "venv\lib\site-packages\allauth\account\utils.py", line 110, in user_field
v = v[0:max_length]
TypeError: 'PhoneNumber' object is not subscriptable
i think this problem because all auth try to subscribe the username by using slicing i recommend to try the following
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
phone_number = models.CharField(validators=[phone_regex], max_length=17, blank=True) # validators should be a list
this token from
What's the best way to store Phone number in Django models
So I want to asynchronously conduct file saves to django file storage. I've tried to achieve this by overwriting the default File System Storage class.
import hashlib
import os
import uuid
import django.core.files.storage as storage
from celery import shared_task
from django.core.files import File
class DefaultStorage(storage.FileSystemStorage):
def __init__(self):
super(DefaultStorage, self).__init__()
#shared_task
def _save(self, name, content):
# need to encrypt file before saving
return super(DefaultStorage, self)._save(name, content)
#shared_task
def _open(self, name, mode='rb'):
# need to decrypt before opening
return File(open(self.path(name), mode))
def get_available_name(self, name, max_length=None):
# we return a hash of the file given,
# in case we miss out on uniqueness, django calls
# the get_alternative_name method
dir_name, file_name = os.path.split(name)
file_root, file_ext = os.path.splitext(file_name)
file_root = hashlib.md5(file_root.encode()).hexdigest()
name = os.path.join(dir_name, file_root + file_ext)
return super(DefaultStorage, self).get_available_name(name, max_length)
def get_alternative_name(self, file_root, file_ext):
# we insert a random uuid hex string into the given
# file name before returning the same back
return '%s%s%s' % (file_root, uuid.uuid4().hex, file_ext)
def save(self, name, content, max_length=None):
# override existing system storage save method
# to ensure that we call the shared task and
# utilize the distributed task queue celery offers
has_chunks = hasattr(content, 'chunks')
name = content.name if not name else name
content = File(content, name) if not has_chunks else content
name = self.get_available_name(name, max_length=max_length)
saved = self._save.delay(self, name, content)
return saved
def open(self, name, mode='rb'):
# override existing system storage open method
# to ensure that we call the shared task and
# utilize the distributed task queue celery offers
return self._open.delay(name, mode)
The idea is to override the existing save method to fire off the shared_task named _save. However, I am facing the following error while trying to do the same.
Traceback (most recent call last):
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\rest_framework\generics.py", line 242, in post
return self.create(request, *args, **kwargs)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\rest_framework\mixins.py", line 19, in create
self.perform_create(serializer)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\rest_framework\mixins.py", line 24, in perform_create
serializer.save()
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\rest_framework\serializers.py", line 205, in save
self.instance = self.create(validated_data)
File "C:\Users\iyapp\PycharmProjects\cloud-server\api_backend\serializers\partial.py", line 32, in create
return super(PartialUploadSerializer, self).create(validated_data)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\rest_framework\serializers.py", line 939, in create
instance = ModelClass._default_manager.create(**validated_data)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\query.py", line 447, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\iyapp\PycharmProjects\cloud-server\api_backend\models\uploads.py", line 30, in save
super(Upload, self).save(*args, **kwargs)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\base.py", line 754, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\base.py", line 792, in save_base
force_update, using, update_fields,
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\base.py", line 895, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\base.py", line 935, in _do_insert
using=using, raw=raw,
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\query.py", line 1254, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1396, in execute_sql
for sql, params in self.as_sql():
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1341, in as_sql
for obj in self.query.objs
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1341, in <listcomp>
for obj in self.query.objs
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1340, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1291, in pre_save_val
return field.pre_save(obj, add=True)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\fields\files.py", line 307, in pre_save
file.save(file.name, file.file, save=False)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\django\db\models\fields\files.py", line 87, in save
self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "C:\Users\iyapp\PycharmProjects\cloud-server\api_backend\models\storage\default_storage.py", line 48, in save
saved = self._save.delay(self, name, content)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\celery\app\task.py", line 421, in delay
return self.apply_async(args, kwargs)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\celery\app\task.py", line 565, in apply_async
**options
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\celery\app\base.py", line 749, in send_task
amqp.send_task_message(P, name, message, **options)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\celery\app\amqp.py", line 532, in send_task_message
**properties
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\kombu\messaging.py", line 166, in publish
compression, headers)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\kombu\messaging.py", line 249, in _prepare
body) = dumps(body, serializer=serializer)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\kombu\serialization.py", line 220, in dumps
payload = encoder(data)
File "C:\Users\iyapp\AppData\Local\Programs\Python\Python36-32\lib\contextlib.py", line 99, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\kombu\serialization.py", line 53, in _reraise_errors
reraise(wrapper, wrapper(exc), sys.exc_info()[2])
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\kombu\exceptions.py", line 21, in reraise
raise value.with_traceback(tb)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\kombu\serialization.py", line 49, in _reraise_errors
yield
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\kombu\serialization.py", line 220, in dumps
payload = encoder(data)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\kombu\utils\json.py", line 66, in dumps
**dict(default_kwargs, **kwargs))
File "C:\Users\iyapp\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:\Users\iyapp\AppData\Local\Programs\Python\Python36-32\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\iyapp\AppData\Local\Programs\Python\Python36-32\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\iyapp\PycharmProjects\cloud-server\venv\lib\site-packages\kombu\utils\json.py", line 55, in default
return super().default(o)
File "C:\Users\iyapp\AppData\Local\Programs\Python\Python36-32\lib\json\encoder.py", line 180, in default
o.__class__.__name__)
kombu.exceptions.EncodeError: Object of type 'DefaultStorage' is not JSON serializable
I believe I have found the source of the issue. It is because we cannot pass "file" objects as parameters to shared_task functions. If this is the case, then how can I send my file to the _save function?
Before sending, the file isn't saved yet. We need to save the file only after receiving.
I have a django app which is running on 1.6 with python 3. Recently I have added django-storages to my app.
When I am trying to save a model(record) which worked well previously, its showing up the below error
Traceback (most recent call last):
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/core/handlers/base.py", line 112, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/contrib/admin/options.py", line 432, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/utils/decorators.py", line 99, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/views/decorators/cache.py", line 52, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 198, in inner
return view(request, *args, **kwargs)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/utils/decorators.py", line 29, in _wrapper
return bound_func(*args, **kwargs)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/utils/decorators.py", line 99, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/utils/decorators.py", line 25, in bound_func
return func(self, *args2, **kwargs2)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/transaction.py", line 371, in inner
return func(*args, **kwargs)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/contrib/admin/options.py", line 1131, in add_view
self.save_model(request, new_object, form, False)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/contrib/admin/options.py", line 860, in save_model
obj.save()
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/base.py", line 545, in save
force_update=force_update, update_fields=update_fields)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/base.py", line 573, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/base.py", line 654, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/base.py", line 687, in _do_insert
using=using, raw=raw)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/manager.py", line 232, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/query.py", line 1514, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 902, in execute_sql
for sql, params in self.as_sql():
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 860, in as_sql
for obj in self.query.objs
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 860, in <listcomp>
for obj in self.query.objs
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 858, in <listcomp>
for f in fields
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/fields/files.py", line 252, in pre_save
file.save(file.name, file, save=False)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/fields/files.py", line 87, in save
setattr(self.instance, self.field.name, self.name)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/fields/files.py", line 311, in __set__
self.field.update_dimension_fields(instance, force=True)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/fields/files.py", line 381, in update_dimension_fields
width = file.width
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/core/files/images.py", line 17, in _get_width
return self._get_image_dimensions()[0]
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/core/files/images.py", line 27, in _get_image_dimensions
self.open()
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/db/models/fields/files.py", line 76, in open
self.file.open(mode)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/core/files/base.py", line 121, in open
if not self.closed:
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/django/core/files/base.py", line 59, in _get_closed
return not self.file or self.file.closed
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/storages/backends/s3boto.py", line 406, in _get_file
self.key.get_contents_to_file(self._file)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/boto/s3/key.py", line 1643, in get_contents_to_file
response_headers=response_headers)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/boto/s3/key.py", line 1475, in get_file
query_args=None)
File "/Users/david/.virtualenvs/project/lib/python3.4/site-packages/boto/s3/key.py", line 1529, in _get_file_internal
fp.write(bytes)
TypeError: string argument expected, got 'bytes'
model
class RequisitionImage(models.Model, ImageRisizeToolbox):
image_id = models.AutoField(primary_key=True)
requisition = models.ForeignKey(Requisition)
description = models.CharField(max_length=80)
image_file = models.ImageField(upload_to=get_file_path, height_field='image_height', width_field='image_width')
thumbnail_file = models.ImageField(upload_to=get_file_path, blank=True, null=True)
sort_order = models.SmallIntegerField(blank=True, null=True)
filetype = models.CharField(max_length=10, blank=True, null=True)
objects = models.Manager()
cust_objects = RequisitionImageManager()
MAX_SIZE = 4000000 # Image file size limit
def __str__(self):
return self.description
I am getting this error when i am trying to save the RequisitionImage object record both from the admin model and from my view.
So how to avoid this error and what is causing it ?
I updated the info below to reference a different model/view that is giving me the same error. Its a simpler model so there are less variables to take into account...
I have the following model:
class Imaging_order(Order):
order_description = models.ForeignKey(Imaging_test, limit_choices_to = {'active': 1}, null=True, blank=True)
orders = models.ManyToManyField(Imaging_test, limit_choices_to = {'active': 1}, related_name='orders')
...
I have the following views (basically the generic django class based update view with a couple custom functions mixed in):
class LoginRequiredMixin(object):
#method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)
class EditMixin(object):
def form_valid(self, form):
messages.success(self.request, "This %s has been updated." % (self.model._meta.verbose_name), extra_tags='msg')
return super(EditMixin, self).form_valid(form)
class ImagingMixin(LoginRequiredMixin):
model = Imaging_order
form_class = ImagingForm
class ImagingUpdateView(ImagingMixin, EditMixin, UpdateView):
pass
On our live version I occassionally get notices of this error:
Traceback (most recent call last):
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/base.py", line 47, in view
return self.dispatch(request, *args, **kwargs)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/utils/decorators.py", line 28, in _wrapper
return bound_func(*args, **kwargs)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/utils/decorators.py", line 24, in bound_func
return func(self, *args2, **kwargs2)
File "/home/cpcadmin/webapps/cpcdj2/cpc/emr/views.py", line 150, in dispatch
return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/base.py", line 68, in dispatch
return handler(request, *args, **kwargs)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/edit.py", line 195, in post
return super(BaseUpdateView, self).post(request, *args, **kwargs)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/edit.py", line 138, in post
return self.form_valid(form)
File "/home/cpcadmin/webapps/cpcdj2/cpc/emr/views.py", line 155, in form_valid
return super(EditMixin, self).form_valid(form)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/edit.py", line 112, in form_valid
self.object = form.save()
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/forms/models.py", line 363, in save
fail_message, commit, construct=False)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/forms/models.py", line 86, in save_instance
save_m2m()
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/forms/models.py", line 82, in save_m2m
f.save_form_data(instance, cleaned_data[f.name])
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/fields/related.py", line 1149, in save_form_data
setattr(instance, self.attname, data)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/fields/related.py", line 746, in __set__
manager.add(*value)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/fields/related.py", line 503, in add
self._add_items(self.source_field_name, self.target_field_name, *objs)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/fields/related.py", line 587, in _add_items
'%s_id' % target_field_name: obj_id,
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/query.py", line 360, in create
obj.save(force_insert=True, using=self.db)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/base.py", line 460, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/base.py", line 553, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/query.py", line 1436, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/sql/compiler.py", line 791, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/sql/compiler.py", line 735, in execute_sql
cursor.execute(sql, params)
File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)
File "/usr/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
IntegrityError: (1062, "Duplicate entry '25129-18' for key 2")
I cannot tell when this error is occurring (usually this view works just fine). I think I have a normal model/view/form setup except that I extended the generic class-based views a bit. I agree with the below comment that this is related to a m2m field. Every time this error comes up it is calling "save_m2m", which then results in this error. I still can't reproduce it. It occurs from my update page/view so I think its only when updating an existing model with existing related records. This is the closest other post I could find, but was of no help: IntegrityError: (1062, "Duplicate entry '1830327-1792993' for key 'some_instance_A_id'") but no UNIQUE constraint