Ari M
Ari M

Reputation: 1426

django allways outputs a model field to PostgreSQL "character varying(200)"

I new to django and PostgreSQL and I have a model that needs a URL so I used a URLField, but one of the URLs that it tries to save is longer then 200 which causes a:

django.db.utils.DataError: value too long for type character varying(200)

I tried changing the max_legnth to 255 or the URLField to TextField but it always translates to type character varying(200) on PostgreSQL.

How can i address this issue?

Don't know if it matters but i use south.

Upvotes: 2

Views: 1420

Answers (3)

Lauri Elias
Lauri Elias

Reputation: 1299

For me, the specific value too long for type character varying(200) occurs when I have a __unicode__ method defined in my model that may print out something longer than 200 symbols and I try to manipulate an instance in the admin view.

My 'fix' was to replace this:

def __unicode__(self):
    return u'%s - %s - %s' % (self.photo.title, self.photo.desc, self.user.fb_name)

With this:

def __unicode__(self):
    title = None
    desc = None
    if self.photo.title:
        title = self.photo.title[:50]
    if self.photo.description:
        desc = self.photo.description[:50]
    return u'%s - %s - %s' % (title, desc, self.user.fb_name)

Upvotes: 0

Nagasaki45
Nagasaki45

Reputation: 2850

As Evan Porter noted, you need to create new schema migration and then migrate.

Run:

python manage.py schemamigration <APP_NAME> --auto
python manage.py migrate

It should solve the problem

Upvotes: 2

Yogesh dwivedi Geitpl
Yogesh dwivedi Geitpl

Reputation: 4462

you should use TextField if you don't care length limitation of URL, with URLValidator

from django.core.validators import URLValidator
field = models.TextField(validators=[URLValidator()])

This behave like url field with. after doing this you have to migrate your model using south.

Upvotes: 0

Related Questions