简体   繁体   中英

Dynamic length Django model field

I have one Django model field definition like this:

landMark = models.CharField(max_length=50, blank=True)

Can I define the landMark field dynamically so that it is capable of holding a string which has a varying size ?

In a way, you already have. The field you have declared is capable of storing a string of any length, as long as it does not exceed 50 characters.

Now, if you want to be able to store an unlimited amount of characters, you can also use a TextField . For example:

landMark = models.TextField(blank=True)

No you can't. The length of string that can be accepted relies on the structure of your database -> you'd have to migrate your database on every max_length change! The solution to your problem would be to simply measure a potential maximum size of variable you'd like to save (here I think about some dev environment). Then set max_length accordingly.

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