I have a trouble with unicode filenames on production server. When I'm trying to save file UnicodeEncodeError exception occures. Devserver works fine.
All locales for www-data are 'en_US.UTF-8'.
Also, I wrote down in /etc/apache2/envvars
export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'
it did not help.
locale.getdefaultlocale() was returning (None,None), then I add following lines to django.wsgi
os.environ['LANG']='en_US.UTF-8'
os.environ['LC_ALL']='en_US.UTF-8'
After it locale.getdefaultlocale() started to return ('en_US', 'UTF8')
I noticed that sys.getdefaultencoding() returns 'ascii' but I don't know how to fix it.
Traceback:
Traceback:
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/contrib/admin/options.py" in wrapper
366. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/contrib/admin/sites.py" in inner
196. return view(request, *args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/utils/decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/utils/decorators.py" in bound_func
21. return func(self, *args2, **kwargs2)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/transaction.py" in inner
209. return func(*args, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/contrib/admin/options.py" in add_view
955. self.save_model(request, new_object, form, False)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django_pencil/admin.py" in save_model
52. super(PictureAdmin, self).save_model(request, obj, form, change)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/contrib/admin/options.py" in save_model
709. obj.save()
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/base.py" in save
463. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/base.py" in save_base
551. result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/manager.py" in _insert
203. return insert_query(self.model, objs, fields, **kwargs)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/query.py" in insert_query
1576. return query.get_compiler(using=using).execute_sql(return_id)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/sql/compiler.py" in execute_sql
909. for sql, params in self.as_sql():
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/sql/compiler.py" in as_sql
872. for obj in self.query.objs
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/fields/files.py" in pre_save
249. file.save(file.name, file, save=False)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django_resized/forms.py" in save
31. super(ResizedImageFieldFile, self).save(name, new_content, save)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/db/models/fields/files.py" in save
86. self.name = self.storage.save(name, content)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/core/files/storage.py" in save
44. name = self.get_available_name(name)
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/core/files/storage.py" in get_available_name
70. while self.exists(name):
File "/home/myproject/myproject.com/python/lib/python2.5/site-packages/django/core/files/storage.py" in exists
230. return os.path.exists(self.path(name))
File "/usr/lib/python2.5/posixpath.py" in exists
171. st = os.stat(path)
Exception Type: UnicodeEncodeError at /admin/django_pencil/picture/add/
Exception Value: 'ascii' codec can't encode characters in position 58-65: ordinal not in range(128)
also add to django.wsgi:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
simple sys.setdefaultencoding('utf-8') will not help, reload is important
This is python 2.x problem, not the django
Debian doesn't I believe use '/etc/apache2/envvars'. Setting these values from inside of the Python script will not work as it will not initialise some things that only occur at process startup.
You either need to set the variables in the init.d startup script for Apache if envvars not consulted, or use mod_wsgi 3.4 and use daemon mode, setting the 'lang' and 'locale' options to WSGIDaemonProcess directive.
Related
I face a vicious cycle that I cannot break out of;
Lets say I have a following model:
class AgregatorProductCoupon(models.Model):
AgregatorProductId = models.ManyToManyField(Product, related_name="coupons", db_column='AgregatorProductId', verbose_name=_("Agregator Product Id"))
class Meta:
managed = True
db_table = 'AgregatorProductCoupon'
when I try to save an instance, I get an integrity error stating
"<AgregatorProductCoupon: AgregatorProductCoupon object (None)>" needs to have a value for field "id" before this many-to-many relationship can be used.
but when I try to save without the m2m rel, I obviously get an error for a required field missing.
Cannot insert the value NULL into column 'AgregatorProductId', table 'test_dbo.dbo.AgregatorProductCoupon'; column does not allow nulls. INSERT fails.
I cannot redesign the tables as they are bound to other places too.
How to go about it?
Any help would be much appreciated.
EDIT: Traceback:
[2021-01-07 13:19:28,040] log_response - Internal Server Error: /admin/agregator/agregatorcoupons/78/change/
Traceback (most recent call last):
File "C:\David\.venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\David\.venv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\David\.venv\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\options.py", line 607, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\David\.venv\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\David\.venv\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\sites.py", line 223, in inner
return view(request, *args, **kwargs)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\options.py", line 1650, in change_view
return self.changeform_view(request, object_id, form_url, extra_context)
File "C:\David\.venv\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "C:\David\.venv\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\options.py", line 1536, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\options.py", line 1576, in _changeform_view
self.save_related(request, form, formsets, not add)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\options.py", line 1122, in save_related
self.save_formset(request, form, formset, change=change)
File "C:\David\.venv\lib\site-packages\django\contrib\admin\options.py", line 1110, in save_formset
formset.save()
File "C:\David\.venv\lib\site-packages\django\forms\models.py", line 669, in save
return self.save_existing_objects(commit) + self.save_new_objects(commit)
File "C:\David\.venv\lib\site-packages\django\forms\models.py", line 803, in save_new_objects
self.new_objects.append(self.save_new(form, commit=commit))
File "C:\David\.venv\lib\site-packages\django\forms\models.py", line 949, in save_new
obj.save()
File "C:\David\agregator\models.py", line 2745, in save
print(self.AgregatorProductId)
File "C:\David\.venv\lib\site-packages\django\db\models\fields\related_descriptors.py", line 526, in __get__
return self.related_manager_cls(instance)
File "C:\David\.venv\lib\site-packages\django\db\models\fields\related_descriptors.py", line 821, in __init__
raise ValueError('"%r" needs to have a value for field "%s" before '
ValueError: "<AgregatorProductCoupon: AgregatorProductCoupon object (None)>" needs to have a value for field "id" before this many-to-many relationship can be used.
I want to store the image files in the S3 and the video files in the Vimeo in my django project. I am S3Boto3Storage of Django-storage as the storage backend. As Vimeo takes care of compression and content delivery for video files and it's cheap compared to S3, I want to use that for video files.
I thought of uploading the file to the Vimeo using the upload_to attribute in the FileField of django model, but I can't able to get the absolute path of the video file in the method.
here is my implementation of the upload_to method,
import vimeo
def vimeo_client():
client = vimeo.VimeoClient(
token=settings.VIMEO_ACCESS_TOKEN,
key=settings.VIMEO_CLIENT_ID,
secret=settings.VIMEO_CLIENT_SECRET
)
return client
def get_media_path(instance, filename):
new_filename = '{}.{}'.format(uuid.uuid4(), filename.split('.')[-1])
if instance.type == 'image':
file_path = 'posts/media/images/{filename}'.format(
filename=new_filename)
else:
client = vimeo_client()
file_path = client.upload(
filename,
data={
'name': instance.title,
'description': instance.description
}
)
logger.info("File uploaded to: [{}]".format(file_path))
return file_path
But it's giving me this error
OSError: [Errno 2] No such file or directory: 'videoplayback.mp4'
Or I could customise the S3Boto3Storage for my purpose. I would like to know what would be the better solution.
Edit
Here is the full stack trace
Traceback (most recent call last):
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 157, in __call__
response = self.get_response(request)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/core/handlers/base.py", line 124, in get_response
response = self._middleware_chain(request)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/core/handlers/exception.py", line 43, in inner
response = response_for_exception(request, exc)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/core/handlers/exception.py", line 93, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/core/handlers/exception.py", line 139, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/Users/rohanroy/development/django/zappa/appcore/src/appcore/wsgi.py", line 24, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/contrib/admin/options.py", line 551, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 224, in inner
return view(request, *args, **kwargs)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1508, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1408, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1448, in _changeform_view
self.save_model(request, new_object, form, not add)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/contrib/admin/options.py", line 979, in save_model
obj.save()
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/db/models/base.py", line 808, in save
force_update=force_update, update_fields=update_fields)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/db/models/base.py", line 838, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/db/models/base.py", line 924, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/db/models/base.py", line 963, in _do_insert
using=using, raw=raw)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/db/models/query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1112, in execute_sql
for sql, params in self.as_sql():
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1065, in as_sql
for obj in self.query.objs
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1014, in pre_save_val
return field.pre_save(obj, add=True)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/db/models/fields/files.py", line 296, in pre_save
file.save(file.name, file.file, save=False)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/db/models/fields/files.py", line 93, in save
name = self.field.generate_filename(self.instance, name)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/django/db/models/fields/files.py", line 327, in generate_filename
filename = self.upload_to(instance, filename)
File "/Users/rohanroy/development/django/zappa/appcore/src/utils/common.py", line 50, in get_media_path
'description': instance.description
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/vimeo/upload.py", line 49, in upload
filesize = self.__get_file_size(filename)
File "/Users/rohanroy/development/django/zappa/arete/lib/python2.7/site-packages/vimeo/upload.py", line 163, in __get_file_size
return os.path.getsize(filename)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.py", line 57, in getsize
return os.stat(filename).st_size
OSError: [Errno 2] No such file or directory: 'videoplayback.mp4'
Vimeo API's supports 3 types of video upload strategies which are discussed here. The tus approach assumes that the video file is in the local system. To upload video from the browser one need to use the form-based uploads approach which is not useful for an API only server. There is two option to tackle this,
Upload the video from the client-side to the API server using your own API endpoint and then use vimeo.py to upload it to the Vimeo and delete the actual video file from the server.
Use vimeo.js to upload the video file from the client side and then store the returned video_uri in the backend.
I am using Django 1.7 and for authentication I am using Django allauth. For sending email, I started using zoho smtp server. It is able to send normal & transactional mails but it cannot send signup conversation email. It shows the error:
SMTPDataError at /accounts/signup/
(553, b'Relaying disallowed as webmaster#localhost')
The traceback is :
69. return self.dispatch(request, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\utils\decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\views\decorators\debug.py" in sensitive_post_parameters_wrapper
76. return view(request, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\utils\decorators.py" in bound_func
25. return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Users\sp\industryo\allauth\account\views.py" in dispatch
167. return super(SignupView, self).dispatch(request, *args, **kwargs)
File "C:\Users\sp\industryo\allauth\account\views.py" in dispatch
62. **kwargs)
File "C:\Users\sp\industryo\allauth\account\views.py" in dispatch
145. **kwargs)
File "C:\Python34\lib\site-packages\django\views\generic\base.py" in dispatch
87. return handler(request, *args, **kwargs)
File "C:\Users\sp\industryo\allauth\account\views.py" in post
78. response = self.form_valid(form)
File "C:\Users\sp\industryo\allauth\account\views.py" in form_valid
183. self.get_success_url())
File "C:\Users\sp\industryo\allauth\account\utils.py" in complete_signup
162. signal_kwargs=signal_kwargs)
File "C:\Users\sp\industryo\allauth\account\utils.py" in perform_login
123. send_email_confirmation(request, user, signup=signup)
File "C:\Users\sp\industryo\allauth\account\utils.py" in send_email_confirmation
291. signup=signup)
File "C:\Users\sp\industryo\allauth\account\models.py" in send_confirmation
60. confirmation.send(request, signup=signup)
File "C:\Users\sp\industryo\allauth\account\models.py" in send
137. ctx)
File "C:\Users\sp\industryo\allauth\account\adapter.py" in send_mail
100. msg.send()
File "C:\Python34\lib\site-packages\django\core\mail\message.py" in send
286. return self.get_connection(fail_silently).send_messages([self])
File "C:\Python34\lib\site-packages\django\core\mail\backends\smtp.py" in send_messages
99. sent = self._send(message)
File "C:\Python34\lib\site-packages\django\core\mail\backends\smtp.py" in _send
115. self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
File "C:\Python34\lib\smtplib.py" in sendmail
800. raise SMTPDataError(code, resp)
Exception Type: SMTPDataError at /accounts/signup/
Exception Value: (553, b'Relaying disallowed as webmaster#localhost')
What is the issue here? How can I solve this problem?
Change DEFAULT_FROM_EMAIL in your settings. The error message is because your email provider does not accept the default value, webmaster#localhost.
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
Should 'save_as' work with inlines. I'm rather baffled by the traceback but it doesn't seem to show much involvement of my own code.
The problem seems to be the id's of inlines being set to '' when saving an add form. This is handled quite happily by when you do a normal 'add':
Traceback:
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
307. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
93. response = view_func(request, *args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
79. response = view_func(request, *args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
197. return view(request, *args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
28. return bound_func(*args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
93. response = view_func(request, *args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
24. return func(self, *args2, **kwargs2)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/db/transaction.py" in inner
217. res = func(*args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
879. prefix=prefix, queryset=inline.queryset(request))
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/forms/models.py" in __init__
682. queryset=qs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/forms/models.py" in __init__
415. super(BaseModelFormSet, self).__init__(**defaults)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/forms/formsets.py" in __init__
47. self._construct_forms()
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/forms/formsets.py" in _construct_forms
108. self.forms.append(self._construct_form(i))
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/forms/models.py" in _construct_form
691. form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/forms/models.py" in _construct_form
437. connection=connections[self.get_queryset().db])
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/db/models/fields/subclassing.py" in inner
53. return func(*args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/db/models/fields/subclassing.py" in inner
53. return func(*args, **kwargs)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_db_prep_lookup
306. value = self.get_prep_lookup(lookup_type, value)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
292. return self.get_prep_value(value)
File "/Users/andybaker/.virtualenvs/nmdc_test/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
479. return int(value)
Exception Type: ValueError at /admin/property_manager/property/add/
Exception Value: invalid literal for int() with base 10: ''
Could be related to this if you're using FileFields? https://code.djangoproject.com/ticket/14760