I have the following form in a Django project:
class MaterialSearchForm(f.Form):
keyword = f.CharField(
widget=f.TextInput(),
required=False,
label= _(‘Keyword’)
)
subjects = f.ChoiceField(
widget=f.RadioSelect(),
choices=Subject.get_subject_choices(),
required=False,
label=_(‘Subjects’)
)
category = f.ChoiceField(
widget=f.Select(),
choices=Category.get_category_choices(),
required=False,
label=_(‘Category’)
)
level_from = f.ChoiceField(
widget=f.Select(),
choices=[(‘’, ‘-’)] + Material.LEVELS,
required=False,
label=_(‘Level From’)
)
level_to = f.ChoiceField(
widget=f.Select(),
choices=[(‘’, ‘-’)] + Material.LEVELS,
required=False,
label=_(‘Level To’)
)
If I set the project up fresh and I try to run makemigrations, I get an error from calling Subject and Category. When I call makemigrations, no tables exist in the database and so when Django comes upon Subject or Category, it looks them up in the database and fails.
How can I call the classes Subject and Category access their methodes in that form without preventing makemigrations from executing properly?
To solve your immediate problem. Dynamic choices like this need to be set by overriding the __init__ method of the form and set the choices there.
However I do have the following questions
- Are you commiting your migration files to source control?
- Have you explored ModelChoiceField’s fields for the fields you have choices set?
No, what is that?
I’m not sure I understand the question?
Ok let’s take a step back. Where are you trying to run makemigrations? On your local machine or elsewhere?
This is the reference for the ModelChoiceField: Form fields | Django documentation | Django
I ran the command on my local machine. But what difference does it make?
I was ruling out a possible problem due to the forum category you picked. makemigrations should only be run on your local machine and the files should be committed to your version control system of choice (typically git) and migrate is the command to run in multiple environments
Ah ok, source control is something like git. So far I’m deploying manually. Automating deployment is next on my list.
You ought to use something like git regardless of manual or automated deployments.
I think I might slowly be getting to the point where git might be a helpful thing, but I don’t see it as a necessity. I think discipline and proper self organization can get you quite far. Also every additional tool also brings additional problems and overhead.
Just wait until you delete a file from a deployed system, with no way of easily getting the contents back (and the days/months of work that went into that file). This is where git comes in and saves you
. This happened to me early in my career and while there was a back up of the overall machine to recover the file, this was a painful way to learn the lesson!
I would almost agree with this, except that I would word it as “git is an amazingly helpful thing.” True, it’s not a necessity, but it has saved me countless hours of work.
Aside from the “backup”-type usage describe above, it’s also a great tool to use when you want to experiment in your code, and not lose track of your current production code - and to help facilitate merging those experiments into your code for deployment.
I do a lot of work where I might have a system in production, but want to play with some ideas that aren’t ready to deploy. I can create a branch, or multiple branches for different experiments, and make changes without worrying about having an adverse effect on the production system. I can also continue to work on the production system, and use git to help me merge those changes out to my experimental branches to help ensure that those experiments can get merged back into the main branch with a minimum of problems.