#10523 closed Bug (needsinfo)
Changing object with long __repr__ causes error
Reported by: | bromer | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 1.2-beta |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Editing an item in the django administration causes an DataError with the following message:
value too long for type character varying(200)
I don't think the admin should explode, but rather cut the object.__repr__()
Attachments (2)
Change History (17)
comment:1 by , 16 years ago
comment:2 by , 16 years ago
milestone: | → 1.1 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 16 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
This is mostly likely a bug in the reporter's models, not in Django (otherwise we'd be hearing a *lot* more about it). Bromer, if you can track it down to a bug within Django, please feel free to reopen with the full traceback.
comment:4 by , 15 years ago
milestone: | 1.1 |
---|---|
Resolution: | worksforme |
Status: | closed → reopened |
Version: | 1.0 → 1.2-beta |
I have exactly the same problem. I have a model which looks like this:
class Link(models.Model):
title = models.CharField(max_length=500)
def unicode(self):
return self.title
When I try to delete an object with a long title in admin I get an error described above (traceback is attached).
follow-up: 6 comment:5 by , 15 years ago
andreyfedoseev:
That last line: "Exception Value: value too long for type character varying(200)" looks to me like your db column has something wrong already. Your model above is supposed to be a varchar(500) but pgsql thinks its max is 200. Did you perhaps change the max_length value at some point without altering your db tables? If so, that'd be an easy way to make Django try and insert values which it thinks are valid while pgsql thinks they're invalid....
All in all, I can't reproduce this error currently. Maybe you could provide more details, or better yet provide a unit test that triggers it?
follow-up: 7 comment:6 by , 15 years ago
Replying to gabrielhurley:
I believe the problem is in the admin change log. LogEntry in django.contrib.admin.models is:
class LogEntry(models.Model): action_time = models.DateTimeField(_('action time'), auto_now=True) user = models.ForeignKey(User) content_type = models.ForeignKey(ContentType, blank=True, null=True) object_id = models.TextField(_('object id'), blank=True, null=True) object_repr = models.CharField(_('object repr'), max_length=200) action_flag = models.PositiveSmallIntegerField(_('action flag')) change_message = models.TextField(_('change message'), blank=True) objects = LogEntryManager()
I think the problem will surface whenever you try to do something with an object in admin where the object's representation is longer than 200 chars.
comment:7 by , 15 years ago
Replying to kmtracey:
I think the problem will surface whenever you try to do something with an object in admin where the object's representation is longer than 200 chars.
Ah, makes sense. I had a feeling I might be missing something, which is why I didn't actually close the ticket. Sorry!
follow-up: 9 comment:8 by , 15 years ago
I have been unable to duplicate this issue in 1.1.1 or 1.2_beta. I've tried returning 500+ character strings in unicode and repr. I've deleted and updated records of a newly-created model and an existing model.
It is possible that the bug was fixed in admin/models.py, line 15, which contains the following snippet: object_repr[:200]
comment:9 by , 15 years ago
I was running Python 2.6, Postgresql 8.2 and I found out that my issue was related
to the fact that the original source of the data was from a microsoft word document which was copied to excel and then loaded into the database.
It seems that word was putting in some special characters which caused the issue.
Once I typed in my text manually, I was able to delete the record successfully.
comment:10 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:11 by , 13 years ago
Easy pickings: | unset |
---|---|
UI/UX: | unset |
Most likely the issue here is when __repr__()
returns a unicode, which isn't properly cut down to 200 characters by simply using [:200]
.
comment:12 by , 13 years ago
Like others in this thread, I can't seem to be able to reproduce this problem. The attached patch passes for me on Python with Sqlite. Perhaps this would need to be tested on other backends.
comment:13 by , 13 years ago
Resolution: | → needsinfo |
---|---|
Status: | reopened → closed |
I can run Julien's test case against PostgreSQL and MySQL without seeing the issue. Please provide a complete test case or field value example.
comment:14 by , 11 years ago
Can reproduce this bug: object.description = "RT @NPancheewa: "B.A.P ถึง....อย่างปลอดภัยแล้วค่ะ" กูรอประโยคนี้ทุกครั้งที่มีการเดินทาง #비에이피"
description = models.TextField()
def unicode(self):
return self.description
When I'm trying to remove it shows:
value too long for type character varying(200)
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/pins/pin/342/delete/
Django Version: 1.6.2
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'leaflet',
'djgeojson',
'storages',
'social.apps.django_app.default',
'django_extensions',
'endless_pagination',
'haystack',
'imagekit',
'auth',
'pins')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
- response = wrapped_callback(request, *callback_args, callback_kwargs)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
- return self.admin_site.admin_view(view)(*args, kwargs)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
- response = view_func(request, *args, kwargs)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
- response = view_func(request, *args, kwargs)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
- return view(request, *args, kwargs)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
- return bound_func(*args, kwargs)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
- response = view_func(request, *args, kwargs)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
- return func(self, *args2, kwargs2)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
- return func(*args, kwargs)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in delete_view
- self.log_deletion(request, obj, obj_display)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in log_deletion
- action_flag=DELETION
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/contrib/admin/models.py" in log_action
- e.save()
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/models/base.py" in save
- force_update=force_update, update_fields=update_fields)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
- updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
- result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/models/base.py" in _do_insert
- using=using, raw=raw)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/models/manager.py" in _insert
- return insert_query(self.model, objs, fields, kwargs)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/models/query.py" in insert_query
- return query.get_compiler(using=using).execute_sql(return_id)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
- cursor.execute(sql, params)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
- return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
- return self.cursor.execute(sql, params)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/utils.py" in exit
- six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
- return self.cursor.execute(sql, params)
Exception Type: DataError at /admin/pins/pin/342/delete/
Exception Value: value too long for type character varying(200)
comment:15 by , 11 years ago
@hodzanassredin: I suspect you're running into #22305 -- please check the character-set of your database.
Can you provide the full traceback please?