I have multiple models using foreign key dropdown, I want to customize the foreign key drop down as per the condition. I tried using decorators but dropdown dissapers in add admin form

from .decorators import admin_active_country
.
.
.
@admin_active_country
def formfield_for_foreignkey(self, db_field, request, **kwargs):
    return super().formfield_for_foreignkey(db_field, request, **kwargs)

i have created decorators to achieve below

        if db_field.name == "country":
            kwargs["queryset"] = Country.objects.filter(is_active=True)

here is decorators but when using decorators i cannot see dropdown in admin page it dissapears

def admin_active_country(func):
    print(func,"function priniting")
    @functools.wraps(func)
    def wraps(*args,**kwargs):
         # ags[1] is db_field 
        if args[1].name == "country":

            kwargs["queryset"] =  Country.objects.filter(is_active=True)
        return func(*args,**kwargs)

    return wraps

The below code working fine i want to achieve the same using decorators so that i can apply it to multiple admin by following DRY

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "country":
            kwargs["queryset"] = Country.objects.filter(is_active=True)
        return super().formfield_for_foreignkey(db_field, request, **kwargs)

Please help me out , thank in advance

0 Answers