Login

Unlimited-length CharField

Author:
rfugger
Posted:
January 19, 2011
Language:
Python
Version:
1.2
Score:
0 (after 0 ratings)

Unlimited-length character fields in Postgres perform the same as limited-length fields, and the Postgres manual suggests not arbitrarily limiting these fields. Unfortunately, Django does not provide a way to access unlimited-length character fields except using TextField, which is rendered differently in forms and in the admin, and has different connotations.

LongCharField is a way to avoid putting arbitrary max_length values where they aren't required. It will only work with databases that allow VARCHAR with no numeric parameters, such as Postgres. MySQL won't work.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from django.db import models
from django.utils.translation import ugettext_lazy as _

class LongCharField(models.CharField):
    "A basically unlimited-length CharField."
    description = _("Unlimited-length string")
    
    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = int(1e9)  # Satisfy management validation.
        super(models.CharField, self).__init__(*args, **kwargs)
        # Don't add max-length validator like CharField does.

    def get_internal_type(self):
        # This has no function, since this value is used as a lookup in
        # db_type().  Put something that isn't known by django so it
        # raises an error if it is ever used.
        return 'LongCharField'

    def db_type(self, connection):
        # *** This is probably only compatible with Postgres.
        # 'varchar' with no max length is equivalent to 'text' in Postgres,
        # but put 'varchar' so we can tell LongCharFields from TextFields
        # when we're looking at the db.
        return 'varchar'
    
    def formfield(self, **kwargs):
        # Don't pass max_length to form field like CharField does.
        return super(models.CharField, self).formfield(**kwargs)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

dudus (on January 19, 2011):

It would be better to just stick with TextField and change it's widget. Your solution breaks db portability.

#

martinsmid (on October 21, 2015):

Thanks. There are situations, in which you cannot use TextField instead of CharField. For example, when used with array aggregation, your postgre query might end up like this:

HAVING ARRAY_AGG("media_tag"."name") @> '{bang,bish}'::text[];
ERROR:  operator does not exist: character varying[] @> text[]
LINE 26: HAVING ARRAY_AGG("media_tag"."name") @> '{bang,bish}'::text[...
                                              ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

Thus, this game is also about database types.

#

Please login first before commenting.