Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ChoiceField in Django model

Suppose I have ChoiceField in my Django model which consist of several choices.

Now, in the future if I changed the existing choice (option) with some other name (choice), will the existing records in the model with that choice (option) also change? Do I need to set that new option manually in the admin page?

like image 918
Asif Avatar asked Nov 10 '11 10:11

Asif


People also ask

What is CharField in Django?

CharField is generally used for storing small strings like first name, last name, etc. To store larger text TextField is used. The default form widget for this field is TextInput. CharField has one extra required argument: CharField.max_length. The maximum length (in characters) of the field.

How can we make field required in Django?

Let's try to use required via Django Web application we created, visit http://localhost:8000/ and try to input the value based on option or validation applied on the Field. Hit submit. Hence Field is accepting the form even without any data in the geeks_field. This makes required=False implemented successfully.

How do you remove this field is required Django?

If yes try to disable this behavior, set the novalidate attribute on the form tag As <form action="{% url 'new_page' %}", method="POST" novalidate> in your html file. At you model, the setting title/content = CharField(null=True, blank=True) doesn´t work? At your model-form have you tried setting title/content = forms.

What is initial in Django forms?

initial is used to change the value of the field in the input tag when rendering this Field in an unbound Form. initial accepts as input a string which is new value of field. The default initial for a Field is empty. Let's check how to use initial in a field using a project.


1 Answers

ChoiceFields are stored in the database as the values, so to take an example from the documentation:

class Foo(models.Model):     GENDER_CHOICES = (         ('M', 'Male'),         ('F', 'Female'),     )     gender = models.CharField(max_length=1, choices=GENDER_CHOICES) 

The database will store 'M' and 'F', so if you one day decide to rename those like this*:

class Foo(models.Model):     GENDER_CHOICES = (         ('M', 'Homme'),         ('F', 'Femme'),     )     gender = models.CharField(max_length=1, choices=GENDER_CHOICES) 

Then anywhere you use the expanded values 'Male' or 'Female' will now have 'Homme' or 'Femme'.

If you want to change the values (i.e. 'M' and 'F') themselves, then you'll want to update the database, so if you wanted to change 'M' to 'H', then you'd use update:

Foo.objects.filter(gender = 'M').update(gender = 'H') 

Unless you've got a good reason to, I'd avoid doing this - since you'll need to make sure your change to GENDER_CHOICES and your update query are done simultaneously.

*And yes, this I know this is a stupid way to do translation!

like image 170
Dominic Rodger Avatar answered Sep 18 '22 14:09

Dominic Rodger