Opened 8 years ago

Closed 5 years ago

Last modified 3 years ago

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

traceback.txt (5.0 KB) - added by Andrey Fedoseev 7 years ago.
traceback
10523.logentry-200-chars-repr.diff (2.4 KB) - added by Julien Phalip 5 years ago.
Passing test case

Download all attachments as: .zip

Change History (17)

comment:1 Changed 8 years ago by Alex Gaynor

Can you provide the full traceback please?

comment:2 Changed 8 years ago by Jacob

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:3 Changed 8 years ago by Jacob

Resolution: worksforme
Status: newclosed

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 Changed 7 years ago by Andrey Fedoseev

milestone: 1.1
Resolution: worksforme
Status: closedreopened
Version: 1.01.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).

Changed 7 years ago by Andrey Fedoseev

Attachment: traceback.txt added

traceback

comment:5 Changed 7 years ago by Gabriel Hurley

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?

comment:6 in reply to:  5 ; Changed 7 years ago by Karen Tracey

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 in reply to:  6 Changed 7 years ago by Gabriel Hurley

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!

comment:8 Changed 7 years ago by ShawnMilo

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 in reply to:  8 Changed 6 years ago by jeffhg2758

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 Changed 6 years ago by Chris Beaven

Severity: Normal
Type: Bug

comment:11 Changed 5 years ago by Julien Phalip

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

Changed 5 years ago by Julien Phalip

Passing test case

comment:12 Changed 5 years ago by Julien Phalip

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 Changed 5 years ago by Ramiro Morales

Resolution: needsinfo
Status: reopenedclosed

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 Changed 3 years ago by hodzanassredin@…

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

  1. 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

  1. 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

  1. 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

  1. 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

  1. return view(request, *args, kwargs)

File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper

  1. return bound_func(*args, kwargs)

File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view

  1. 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

  1. return func(self, *args2, kwargs2)

File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/transaction.py" in inner

  1. return func(*args, kwargs)

File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in delete_view

  1. 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

  1. action_flag=DELETION

File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/contrib/admin/models.py" in log_action

  1. e.save()

File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/models/base.py" in save

  1. 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

  1. 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

  1. 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

  1. using=using, raw=raw)

File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/models/manager.py" in _insert

  1. 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

  1. 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

  1. cursor.execute(sql, params)

File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute

  1. 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

  1. return self.cursor.execute(sql, params)

File "/home/hodza/projects/pinstream/venv/local/lib/python2.7/site-packages/django/db/utils.py" in exit

  1. 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

  1. 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 Changed 3 years ago by Shai Berger

@hodzanassredin: I suspect you're running into #22305 -- please check the character-set of your database.

Note: See TracTickets for help on using tickets.
Back to Top