Charfield variable length

23 views
Skip to first unread message

Odile Lambert

unread,
Jan 23, 2019, 5:26:41 PM1/23/19
to django...@googlegroups.com

Hello

I have problems with the Charfield max_length. In the source code, the possibility exists to have max_length = None but it does not pass Django check when  models.py. contains a charfield = None

I can understand that there is a need for such a max_length form the database point of view .
My need is the following:

I have a set of  fields whose content in the database is a binary code of maximum 180 bits. Most of the time it will be much shorter and there is  in the form a field (max size) giving this maximum size.

The user needs to input this field in the database as a Character string of 0 and 1. using hexadecimal fields would be very unpractical. Last but not least, I am using the admin to enter these data in the database.

In a first shot, I used a Charfield and I overwrote the field in the init to set the maximum length to the value chosen by the user. I also modifed the attributes of the widget. But at the end the HTML contains a texinput with the max length set in the model. 

Is this an admin feature or did I miss something?

. Any other suggestion on how to handle these fields would be welcomed.

Mike Dewhirst

unread,
Jan 23, 2019, 7:53:14 PM1/23/19
to django...@googlegroups.com
On 24/01/2019 9:14 am, 'Odile Lambert' via Django users wrote:
>
> Hello
>
> I have problems with the Charfield max_length. In the source code, the
> possibility exists to have max_length = None but it does not pass
> Django check when  models.py. contains a charfield = None
>
> I can understand that there is a need for such a max_length form the
> database point of view .
>

When you run migrate it establishes the size of the Char field *in the
database*. Therefore it can only be set once per migration.

> My need is the following:
>
> I have a set of  fields whose content in the database is a binary code
> of maximum 180 bits. Most of the time it will be much shorter and
> there is  in the form a field (max size) giving this maximum size.
>
> The user needs to input this field in the database as a Character
> string of 0 and 1. using hexadecimal fields would be very unpractical.
> Last but not least, I am using the admin to enter these data in the
> database.
>
> In a first shot, I used a Charfield and*I overwrote the field in the
> init to set the maximum length to the value chosen by the user. I also
> modifed the attributes of the widget. But at the end the HTML contains
> a texinput with the max length set in the model.
> *
>
> Is this an admin feature or did I miss something?
>

A TextField is a varying length character field. Might be of interest.

A binary field requires bytes not text. I think if I needed binary data
entered by a user I would use a CharField with a reasonable max_length
and in the model.save() method I would convert/interpret the data
entered into precisely the binary format I needed and then
programmatically store it into a "parallel" binary field. I would
probably make it readonly in the Admin or just not display it.

I'm not sure about using binary fields with a choice attribute but that
might be another thing to research.

You can certainly use choices in a CharField to totally restrict the
data entered.

> . Any other suggestion on how to handle these fields would be welcomed.
>

It might be easier to offer advice if your use-case was more clear.

> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/23ff10f1-41d5-8da1-18a4-07d457be8756%40laposte.net
> <https://groups.google.com/d/msgid/django-users/23ff10f1-41d5-8da1-18a4-07d457be8756%40laposte.net?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Anirudh Jain

unread,
Jan 23, 2019, 9:11:53 PM1/23/19
to django...@googlegroups.com
If you are sure that values entered will only be 0s and 1s then why not use IntegerField(). But if really want to use CharField() and you know that size won't be larger than 180bits then use CharField(max_lenght=25) 

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

Odile Lambert

unread,
Jan 24, 2019, 3:49:49 AM1/24/19
to django...@googlegroups.com

Hello thank you very much for your answer.

Your answer about the chafield max length is clear.

I do not know how to clarify my use case because it is very special : the binary codes (there are 4) in the database will be used to set pushers on a knitting machine!...So what has a meaning for the user is really the position of the 0 and 1. Thus the idea of entering 0 and 1 characters. And the admin  who creates the database must enter those fields !...

He (she)  also decides what is the max length of these binary codes,  as this is a repeating factor. But physically the machine does not support more than 180 pushers, thus the admin may not enter more than 180 bits (0 and 1 characters) !... which is rather good news for him(she)

Entering 180 0 and 1 characters is very unpractical but most of the time the repeating factor (max length of the binary codes) will be in the order of 10.

There will be no search whatsoever in the database on this field which is just saved and will be used by a GUI to display information to the user and to do calculations. 

I hope this clarifies the need!....

I am now aiming to define a custom smallIntergerfield in the model, with a custom formfield and a textinput widget. As I am rather new to Django this is a fairly high challenge for which I will probably require more help.

If you believe this is a crazy path, please let me know!....

C. Kirby

unread,
Jan 25, 2019, 10:57:39 AM1/25/19
to Django users
Reply all
Reply to author
Forward
0 new messages