Issue #3 open

Suggestion: a second JSON field that inherits from CharField instead of TextField

Tim Molendijk
created an issue

The fact that the current JSON field inherits from TextField is a problem in some scenarios.


I would like to define a database index on a JSON field. Although indexes on TEXT are possible (at least in MySQL), they are not supported by Django as TextField does not offer the db_index attribute. Also, they do not make much sense because there is a limit on the size of an index which lies around 700-1000 characters (depending on the database engine). As a result, a TextField only buys you a few hundred characters over a CharField (which has 255). In most scenarios this is not worth the hassle of having to circumvent the fact that Django does not support them.


So, the most reasonable approach for indexing a JSON field would be to have one that works on top of a CharField instead of a TextField.


I have the sense that adding this second type of JSON field would not be much of a big deal, as I guess it does not involve much more than changing the parent class. I could be missing something though.


Would you consider adding a field like this?

Comments (12)

  1. Tim Molendijk reporter

    Awesome. Well, input box would do just fine actually. 255 characters is plenty for this use case (otherwise it wouldn't make much sense wanting to index it). Also, a benefit of an input box is that it allows enforcing maxlength.

  2. BeProud

    Having to specify the length of the JSONField every time would be a bit cumbersome.

    How would you use an index if you could have one? Text searching over the JSON? In general I think that's kind of impractical.

  3. Tim Molendijk reporter

    Text searching over the JSON indeed. Can be very useful (and practical) for simple JSON structures (such as a list of e-mail addresses).

    The max_length argument can simply be made optional and have a default value of 255.

  4. Raphaƫl Hertzog

    I believe that offering a JSONField that builds on top of the CharField does not make sense. The limitation of index on TextField is certainly gone since that time and newer native JSON type offer smarter indexing and querying features.

    I would close this as wontfix.

  5. Log in to comment