rohitkulky
rohitkulky

Reputation: 1232

Django - Form dropdown list from database not updated immediately after adding new

I am new to Django and am stuck. I have a model sample below -

class Machines(models.Model):
    machine_name = models.CharField(max_length=50,null=True, unique=True)

And associated form -

class NewMachine(forms.Form):
    machine_name = forms.CharField(label="Machine Name",required=True)

Now I am using this model values (names of machine) in a dropdown in another form with -

all_machines = [i['machine_name'] for i in Machines.objects.values('machine_name').distinct()]
machine_choices = [(i,i) for i in get_uniq_obj(all_machines)]
machine_name = forms.MultipleChoiceField(required=False,
                     choices=machine_choices,label="")

get_uniq_obj is just a function that removes duplicates further. The problem is I don't see newly added machine names (from Machines table) in the form dropdown list immediately, or until I restart the server.

I tried to put the all_machines block in various locations but the STDOUT does not go to this code path again for it to load the new values for dropdown from the database.

Appreciate the help.

Upvotes: 1

Views: 477

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599530

You shouldn't do that. Use ModelMultipleChoiceField with the queryset parameter.

Upvotes: 2

Related Questions