def formfield_for_dbfield(self, db_field, **kwargs): """ Override the default widget for Foreignkey fields if they are specified in the related_search_fields class attribute. """ if isinstance(db_field, models.ForeignKey) and db_field.name in self.related_search_fields: help_text = self.get_help_text(db_field.name, db_field.remote_field.model._meta.object_name) if kwargs.get('help_text'): help_text = six.u('%s %s' % (kwargs['help_text'], help_text)) kwargs['widget'] = ForeignKeySearchInput(db_field.remote_field, self.related_search_fields[db_field.name]) kwargs['help_text'] = help_text return super(ForeignKeyAutocompleteAdminMixin, self).formfield_for_dbfield(db_field, **kwargs)
def formfield_for_dbfield(self, db_field, **kwargs): """ Overrides the default widget for Foreignkey fields if they are specified in the related_search_fields class attribute. """ if (isinstance(db_field, models.ForeignKey) and db_field.name in self.related_search_fields): model_name = db_field.rel.to._meta.object_name help_text = self.get_help_text(db_field.name, model_name) if kwargs.get('help_text'): help_text = u'%s %s' % (kwargs['help_text'], help_text) kwargs['help_text'] = help_text widget_kwargs = { 'rel': db_field.rel, 'search_fields': self.related_search_fields[db_field.name], 'admin_site_name': self.admin_site.app_name, } if (isinstance(db_field, models.ForeignKey)): kwargs['widget'] = ForeignKeySearchInput(**widget_kwargs) return super(AutocompleteAdmin, self).formfield_for_dbfield(db_field, **kwargs)
def formfield_for_dbfield(self, db_field, **kwargs): """ Overrides the default widget for Foreignkey fields if they are specified in the related_search_fields class attribute. """ if isinstance(db_field, models.ForeignKey ) and db_field.name in self.related_search_fields: # TODO: Remove me after Django 1.8 is unsupported remote_field = db_field.remote_field if hasattr( db_field, 'remote_field') else db_field.rel remote_field_model = remote_field.model if hasattr( remote_field, 'model') else remote_field.to help_text = self.get_help_text( db_field.name, remote_field_model._meta.object_name) if kwargs.get('help_text'): help_text = six.u('%s %s' % (kwargs['help_text'], help_text)) kwargs['widget'] = ForeignKeySearchInput( remote_field, self.related_search_fields[db_field.name]) kwargs['help_text'] = help_text return super(ForeignKeyAutocompleteAdminMixin, self).formfield_for_dbfield(db_field, **kwargs)