Answer the question
In order to leave comments, you need to log in
Is it possible, is it worth it and if “yes”, then how ... in the model, in addition to the field, immediately indicate the possible values \u200b\u200bof this field and write them to the database during migration?
Good afternoon.
For example, there is
class Country(models.Model):
country_name = models.CharField('Country', max_length = 30)
Answer the question
In order to leave comments, you need to log in
https://docs.djangoproject.com/en/1.8/ref/models/f...
see choices
you can
https://docs.djangoproject.com/en/1.8/ref/models/f...
it's
worth it - the question is to support the normal form of the database
if necessary, and write in the migration the data necessary for the application to work
So, as you already found out, choices are not written to the database - they are stored exclusively in the code and are used by the ORM to validate data and form a list of options in forms, for example, in the Django admin panel.
When the task is set, the following comes to mind:
Use choices and store a list of countries in the project settings - Django settings, this way it will be easier to use the Django ORM features for data validation and data submission in forms. It is worth considering that when adding a new country, you will have to change the settings and restart (reroll) the project.
Then, write the datamigrationto add the countries you need to the database. In such a migration, you can cycle through the country names from the project settings and create all the necessary objects if they have not already been created.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question