2 years ago
#351061
MightyRavendark
Populate Dropdown from another Table/View
I have a site model which is connected to the municipality where the site is located. Using this municipality in a dropdown field in the create form leads to quite long loading times. The reason is the large amount of municipalities in the database but also that the administrative units are linked to each other which seems to lead to cascading queries to populate the dropdown field.
Is there a way to speed this up? I thought about making a materialized view and use it to populate the dropdown field but that would lead to a value error because the value from the dropdown must be an instance of the linked model as far as I know. Is there maybe another way to make the form load faster? Or is the materialized view the way to go and I am just getting something wrong?
class site(models.Model):
sid = models.AutoField(primary_key=True)
site_name = models.CharField(max_length=250)
site_notes = models.CharField(max_length=2500, blank=True, null=True)
municipality = models.ForeignKey('local_administrative_unit', on_delete=models.PROTECT)
geom = models.PointField(srid=4326)
class administrative_level_5(models.Model):
al5_id = models.CharField(primary_key=True, max_length=10)
al5_name = models.CharField(max_length=150)
geom = models.MultiPolygonField(srid=4326)
def __str__(self):
return '{}, {}'.format(self.al5_id, self.al5_name)
class administrative_level_4(models.Model):
al4_id = models.IntegerField(primary_key=True)
al4_name = models.CharField(max_length=150)
adm_level_5 = models.ForeignKey('administrative_level_5', on_delete=models.PROTECT)
geom = models.MultiPolygonField(srid=4326)
def __str__(self):
return '{}, {} ({})'.format(self.al4_id, self.al4_name, self.adm_level_5.al5_name)
class administrative_level_3(models.Model):
al3_id = models.IntegerField(primary_key=True)
al3_name = models.CharField(max_length=150)
adm_level_4 = models.ForeignKey('administrative_level_4', on_delete=models.PROTECT)
geom = models.MultiPolygonField(srid=4326)
def __str__(self):
return '{}, {} ({})'.format(self.al3_id, self.al3_name, self.adm_level_4.al4_name)
class administrative_level_2(models.Model):
al2_id = models.IntegerField(primary_key=True)
al2_name = models.CharField(max_length=150)
adm_level_3 = models.ForeignKey('administrative_level_3',on_delete=models.PROTECT)
geom = models.MultiPolygonField(srid=4326)
def __str__(self):
return '{}, {} ({})'.format(self.al2_id, self.al2_name, self.adm_level_3.al3_name)
class local_administrative_unit(models.Model):
lau_id = models.IntegerField(primary_key=True)
lau_name = models.CharField(max_length=150)
adm_level_2 = models.ForeignKey('administrative_level_2', on_delete=models.PROTECT)
geom = models.MultiPolygonField(srid=4326)
def __str__(self):
return '{}, {} ({})'.format(self.lau_id, self.lau_name, self.adm_level_2.al2_name)
Form looks like this
class NewSiteForm(ModelForm):
class Meta:
model = models.site
fields = (
'site_name',
'site_notes',
'municipality'
)
django
django-forms
dropdown
modelform
0 Answers
Your Answer