Skip to content Skip to sidebar Skip to footer

Remove The Default Select In ForeignKey Field Of Django Admin

There are 150k entries in User model. When i am using it in django-admin without the raw_id_fields it is causing problem while loading all the entries as a select menu of foreign k

Solution 1:

you can use method formfield_for_foreignkey

something like this:

class ProfileRecommendationAdmin(admin.ModelAdmin):


   def formfield_for_foreignkey(self, db_field, request, **kwargs):
     if db_field.name == "user":
       kwargs["queryset"] = User.objects.filter(is_superuser=True)
     return super(ProfileRecommendationAdmin,self).formfield_for_foreignkey(db_field, request, **kwargs)

or other way is to override the form for that modeladmin class

from django import forms
from django.contrib import admin
from myapp.models import Person

class PersonForm(forms.ModelForm):

    class Meta:
        model = Person
        exclude = ['name']

class PersonAdmin(admin.ModelAdmin):
    exclude = ['age']
    form = PersonForm

you can change the widget and use something like the selectize with autocomplete and ajax.


Post a Comment for "Remove The Default Select In ForeignKey Field Of Django Admin"