def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) if hasattr(field, 'rel'): rel_name = field.rel.get_related_field().name else: rel_name = other_model._meta.pk.name self.lookup_formats = { 'in': '%%s__%s__in' % rel_name, 'exact': '%%s__%s__exact' % rel_name } super(RelatedFieldSearchFilter, self).__init__(field, request, params, model, model_admin, field_path) if hasattr(field, 'verbose_name'): self.lookup_title = field.verbose_name else: self.lookup_title = other_model._meta.verbose_name self.title = self.lookup_title self.search_url = model_admin.get_admin_url( '%s_%s_changelist' % (other_model._meta.app_label, other_model._meta.model_name)) self.label = self.label_for_value( other_model, rel_name, self.lookup_exact_val) if self.lookup_exact_val else "" self.choices = '?' if field.rel.limit_choices_to: for i in list(field.rel.limit_choices_to): self.choices += "&_p_%s=%s" % (i, field.rel.limit_choices_to[i]) self.choices = format_html(self.choices)
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) if hasattr(field, "rel"): rel_name = field.rel.get_related_field().name else: rel_name = other_model._meta.pk.name self.lookup_formats = {"in": "%%s__%s__in" % rel_name, "exact": "%%s__%s__exact" % rel_name} super(RelatedFieldSearchFilter, self).__init__(field, request, params, model, model_admin, field_path) related_modeladmin = self.admin_view.admin_site._registry.get(other_model) self.relfield_style = related_modeladmin.relfield_style if hasattr(field, "verbose_name"): self.lookup_title = field.verbose_name else: self.lookup_title = other_model._meta.verbose_name self.title = self.lookup_title self.search_url = model_admin.get_admin_url( "%s_%s_changelist" % (other_model._meta.app_label, other_model._meta.model_name) ) self.label = self.label_for_value(other_model, rel_name, self.lookup_exact_val) if self.lookup_exact_val else "" self.choices = "?" if field.rel.limit_choices_to: for i in list(field.rel.limit_choices_to): self.choices += "&_p_%s=%s" % (i, field.rel.limit_choices_to[i]) self.choices = format_html(self.choices)
def formfield_for_dbfield(self, formfield, db_field, **kwargs): if formfield and self.model in self.admin_site._registry and isinstance(db_field, (models.ForeignKey, models.ManyToManyField)): rel_model = get_model_from_relation(db_field) if rel_model in self.admin_site._registry and self.has_model_perm(rel_model, 'add'): add_url = self.get_model_url(rel_model, 'add') formfield.widget = RelatedFieldWidgetWrapper( formfield.widget, db_field.rel, add_url, self.get_model_url(self.model, 'add')) return formfield
def test(cls, field, request, params, model, admin_view, field_path): if not (hasattr(field, 'rel') and bool(field.rel) or isinstance(field, ForeignObjectRel)): return False related_modeladmin = admin_view.admin_site._registry.get( get_model_from_relation(field)) return related_modeladmin and getattr( related_modeladmin, 'relfield_style', None) == 'fk-ajax'
def formfield_for_dbfield(self, formfield, db_field, **kwargs): if formfield and self.model in self.admin_site._registry and isinstance(db_field, (models.ForeignKey, models.ManyToManyField)) and not getattr(self.admin_view, 'wizard_form_list', None): # disable quick add button in wizard form which will caused problems by Eddie -- TBD rel_model = get_model_from_relation(db_field) if rel_model in self.admin_site._registry and self.has_model_perm(rel_model, 'add'): add_url = self.get_model_url(rel_model, 'add') formfield.widget = RelatedFieldWidgetWrapper( formfield.widget, db_field.rel, add_url, self.get_model_url(self.model, 'add')) return formfield
def formfield_for_dbfield(self, formfield, db_field, **kwargs): if db_field.name in self.quick_addbtn_fields_exclude or \ isinstance(db_field, self.quick_addbtn_db_fields_exclude): return formfield # disabled to this types elif formfield and self.model in self.admin_site._registry and \ isinstance(db_field, (models.ForeignKey, models.ManyToManyField)): rel_model = get_model_from_relation(db_field) if rel_model in self.admin_site._registry and self.has_model_perm( rel_model, 'add'): add_url = self.get_model_url(rel_model, 'add') formfield.widget = RelatedFieldWidgetWrapper( formfield.widget, db_field.rel, add_url, self.get_model_url(self.model, 'add'), **self.request.GET) return formfield
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) if hasattr(field, 'rel'): rel_name = field.rel.get_related_field().name else: rel_name = other_model._meta.pk.name self.lookup_formats = {'in': '%%s__%s__in' % rel_name,'exact': '%%s__%s__exact' % rel_name, 'isnull': '%s__isnull'} self.lookup_choices = field.get_choices(include_blank=False) super(RelatedFieldListFilter, self).__init__( field, request, params, model, model_admin, field_path) if hasattr(field, 'verbose_name'): self.lookup_title = field.verbose_name else: self.lookup_title = other_model._meta.verbose_name self.title = self.lookup_title
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) if hasattr(field, "rel"): rel_name = field.rel.get_related_field().name else: rel_name = other_model._meta.pk.name self.lookup_formats = { "in": "%%s__%s__in" % rel_name, "exact": "%%s__%s__exact" % rel_name, "isnull": "%s__isnull", } self.lookup_choices = field.get_choices(include_blank=False) super(RelatedFieldListFilter, self).__init__(field, request, params, model, model_admin, field_path) if hasattr(field, "verbose_name"): self.lookup_title = field.verbose_name else: self.lookup_title = other_model._meta.verbose_name self.title = self.lookup_title
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) if hasattr(field, 'rel'): rel_name = field.rel.get_related_field().name else: rel_name = other_model._meta.pk.name self.lookup_formats = { 'in': '%%s__%s__in' % rel_name, 'exact': '%%s__%s__exact' % rel_name, 'isnull': '%s__isnull' } self.lookup_choices = field.get_choices(include_blank=False) super(RelatedFieldListFilter, self).__init__(field, request, params, model, model_admin, field_path) if hasattr(field, 'verbose_name'): self.lookup_title = field.verbose_name else: self.lookup_title = other_model._meta.verbose_name self.title = self.lookup_title
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) if hasattr(field, 'rel'): rel_name = field.rel.get_related_field().name else: rel_name = other_model._meta.pk.name self.lookup_formats = {'in': '%%s__%s__in' % rel_name,'exact': '%%s__%s__exact' % rel_name} super(RelatedFieldSearchFilter, self).__init__( field, request, params, model, model_admin, field_path) if hasattr(field, 'verbose_name'): self.lookup_title = field.verbose_name else: self.lookup_title = other_model._meta.verbose_name self.title = self.lookup_title self.search_url = model_admin.get_admin_url('%s_%s_changelist' % ( other_model._meta.app_label, other_model._meta.model_name)) self.label = self.label_for_value(other_model, rel_name, self.lookup_exact_val) if self.lookup_exact_val else "" self.choices = '?' if field.rel.limit_choices_to: for i in list(field.rel.limit_choices_to): self.choices += "&_p_%s=%s" % (i, field.rel.limit_choices_to[i]) self.choices = format_html(self.choices)
def test(cls, field, request, params, model, admin_view, field_path): if not (hasattr(field, 'rel') and bool(field.rel) or isinstance(field, ForeignObjectRel)): return False related_modeladmin = admin_view.admin_site._registry.get( get_model_from_relation(field)) return related_modeladmin and getattr(related_modeladmin, 'relfield_style', None) == 'fk-ajax'
def test(cls, field, request, params, model, admin_view, field_path): if not is_related_field2(field): return False related_modeladmin = admin_view.admin_site._registry.get(get_model_from_relation(field)) return related_modeladmin and getattr(related_modeladmin, "relfield_style", None) in ("fk-ajax", "fk-select")