I'm using Django's built-in Admin functionality to support a web app's admin users.
So far I'm also using mixins and "rest_framework - viewsets" library to have automated a generic CRUD API.
In my admin.py files, I've used fieldsets to easily create a nice form to add new records with automatically included dropdowns, checkboxes, dateboxes etc. Great.
A recent concern is that some of these some item selections should dictate the available options of other items.
For example, if a person selects a state/province, the list of available cities should be restricted to those in just that state/province, instead of all the cities in the database.
Is there any way to implement this behavior within the fieldsets syntax / without having to abandon viewsets and revert to manually coding this behaviour?
Edit #1: To be clear, my working application has no forms.py. So I'm looking for a solution that effects only models.py, admin.py, or views.py
Edit #2: Provided code examples.
Problem: After selecting State, "city" dropdown still shows all available cities, not specific to country selected above.
Sample admin.py
class SiteAdmin(ImportExportModelAdmin):
fieldsets = (
(Region, {
'fields': ('State/Province')
}),
('SubArea, {
'fields': ('City'),
}),
)
Sample views.py
class SiteViewSet(viewsets.ReadOnlyModelViewSet):
"""
A simple ViewSet for viewing accounts.
"""
queryset = Site.objects.all()
serializer_class = SiteSerializer
Sample models.py
class State(models.Model):
name = models.CharField(max_length=100)
class City(models.Model):
name = models.CharField(max_length=100)
state = models.ForeignKey(state, on_delete=models.CASCADE,)