Populate choices from existing values in a model field

10 views
Skip to first unread message

Patrick Carra

unread,
Jun 19, 2020, 6:33:17 PM6/19/20
to Django users
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.

Integr@te System

unread,
Jun 20, 2020, 1:39:03 PM6/20/20
to django...@googlegroups.com
Hi Patrick,

Plz review for your suitable case:

Nice.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b63b6f32-0040-42ee-9021-e45657e9caf7o%40googlegroups.com.

Patrick Carra

unread,
Jun 23, 2020, 2:24:37 PM6/23/20
to Django users
I'm not directly modifying any code in a .py file I am simply adding a value to the database table but I am unable to see the change reflected in my drop down lists until I restart the server.  Is there a better way of populating choices for my drop down menu?  Or could I simply add a cron job that restarts the server periodically(this seems like not a great solution).
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages