I have a very reasonable pagespeed inside the django admin interface when i open my "facility" objects. But if i open one of my "facility addresses" it will take more than 8 seconds to load. I imagine that this is caused by the fact that all existing facilities are being loaded into the dropdown of the OneToMany Field even though the facility is only connected to one address.
How can i limit it so there is either no dropdown on these OneToMany Fields or that it only shows the current objects it is connected to?
class Facility(models.Model):
UUID = models.CharField(max_length=150, null=True, blank=True)
Name = models.CharField(max_length=150, null=True, blank=True)
class Meta:
verbose_name_plural = "facilities"
def __str__(self):
return self.Name
class FacilityAddress(models.Model):
PrimaryAddress = models.CharField(max_length=50, null=True, blank=True)
SecondaryAddress = models.CharField(max_length=50, null=True, blank=True)
City = models.CharField(max_length=50, null=True, blank=True)
RegionOrState = models.CharField(max_length=30, null=True, blank=True)
PostalCode = models.CharField(max_length=20, null=True, blank=True)
Geolocation = models.CharField(max_length=20, null=True, blank=True)
AddressInfo = models.ForeignKey(Facility, null=True, blank=True, on_delete=models.CASCADE, related_name='fa')
class Meta:
verbose_name_plural = "facility addresses"
def __str__(self):
return f"{self.PrimaryAddress} {self.City}"
>Solution :
You should use autocomplete_fields in your admin so that all instances are not loaded by your ForeignKey
admin.py
from django.contrib import admin
from .models import Facility, FacilityAddress
class FacilityAdmin(admin.ModelAdmin):
search_fields = ['Name']
class FacilityAddressAdmin(admin.ModelAdmin):
autocomplete_fields = ['AddressInfo']
admin.site.register(Facility, FacilityAdmin)
admin.site.register(FacilityAddress, FacilityAddressAdmin)