简体   繁体   中英

Best practices for unknown max_length in Django?

After having to increase max_length of another field in a model, I've started to wonder: maybe this is not the way?

I'm getting data from an external API, so I can't check what's the maximum length. Let's say that I'm guessing the field can have 100 chars - because it makes sense, but I have no idea if this is actually the case, there might appear a value which is 300 chars long. What's the recommended approach here?

1) Truncate the value (where should I put the code then? and what with fields such as URL, which won't work after truncating?)? 2) Skip the value? 3) Set length of every field to 100*expected length?

The point of max_length is that you decide it. If you really want variable/unlimited length text, use TextField instead.

The max_length is enforced on database level, If it's longer, it will just fail. If you want to truncate it from code side, you have to do it yourself, most likely in the function where you create the entry.

Note that, many databases optimize text fields, so there's no penalty in performance for varying length text.

Expanding on blue_notes response I'd recommend that you always use TextField instead of CharField unless the datatype has a fundamental maximum length. For example, ISO currency codes are always exactly 3 characters and make sense as a CharField.

In many situations using a max length character field will result in worse performance in the database then than using their unlimited length varchar field if the field length isn't actually fixed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM