python - admin search for generic foreign key django -


i trying put admin search on model in generic foreign key present.

admin.py

class privacysettingsadmin(admin.modeladmin):     fieldsets = (         (none, {             'fields': (                 'field_name',                 'level',              )         }),         (('page/article'), {             'classes': ('grp-collapse grp-open',),             'fields': ('content_type', 'object_id', )         }),     )     autocomplete_lookup_fields = {         'generic': [['content_type', 'object_id']],     }     search_fields = ('object_id',) 

i able search using object_id. want search using feilds of object_id object.

ex. user has foreign key model. want search privacy setting of user using username, first_name, email , last_name.

thanks in advance.

try below codes:

in models.py:

class privacysettings(models.model):     ...     def object_username(self):         return self.object.username     object_username = property(object_username)      def object_first_name(self):         return self.object.first_name     object_first_name = property(object_first_name)      def object_last_name(self):         return self.object.last_name     object_last_name = property(object_last_name)      def object_email(self):         return self.object.email     object_email = property(object_email) 

in admin.py:

class privacysettingsadmin(admin.modeladmin):     model = privacysettings     fieldsets = (         (none, {             'fields': (                 'field_name',                 'level',              )         }),         (('page/article'), {             'classes': ('grp-collapse grp-open',),             'fields': ('content_type', 'object_id', )         }),     )     autocomplete_lookup_fields = {         'generic': [['content_type', 'object_id']],     }     search_fields = ('object_id', 'object_username', 'object_first_name',                      'object_last_name', 'object_email') 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -