Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Only list the connected object from the OneToMany Field instead of all objects

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading