I am trying to read distinct values from a model field and use the tuple list to populate the choices in a Form. The problem I have is that every time a new value is added I must reset the httpd server in order to include the new value in the form. I have researched and tried this a few different ways but so far am unable to come up with a better solution. Any ideas?
models.py
class Circuitinfotable(models.Model):
id1 = models.IntegerField(primary_key=True, blank=True,null=False,unique=True)
pathname = models.CharField(max_length=255, blank=True, null=True)
segmentname = models.CharField(max_length=255, blank=True, null=True) region = models.CharField(max_length=255, blank=True, null=True)
forms.py
def getList(item_list, keyValue):
valueList=[]
for each in item_list:
valueList.append(each[keyValue])
return valueList
def listCreator(field, query):
newList_query = getList(query, field)
sorted_newList = sorted(getUnique(newList_query))
sorted_newList.insert(0, ("", ""))
return sorted_newList
class CircuitinfotableForm(ModelForm):
class Meta:
pathlistQuery = Circuitinfotable.objects.all().values('pathname').exclude(pathname=None).exclude(pathname='None')
path_list = listCreator('pathname', pathlistQuery)
model = Circuitinfotable
fields = ['circuitid', 'pathname', 'segmentname', 'region']
widgets = {'circuitid': forms.TextInput(attrs={'size':40}), 'pathname': forms.Select(choices=path_list), 'segmentname': forms.Select(choices=segment_list),
'region': forms.Select(choices=region_list)}
I shortened this code to only include some of the relevant fields and code for brevity. Here I created two functions to create a list of tuples for pathname from the values I retrieve using a query and pass it to the Select widget using choices.