def get(self, request, app, model, field_path, *args, **kwargs): self.model = self.get_model(app, model) self.fields = get_fields_from_path(self.model, field_path) self.related_model = get_model_from_relation(self.fields[-1]) self.field_path = field_path self.query_string = get_query_string(request.GET, remove=[field_path, 'q']) return super(FilterModelAutocomplete, self).get(request, *args, **kwargs)
def field_choices(self, field, request, model_admin): other_model = get_model_from_relation(field) qs = other_model.objects.exclude(**{field.related_query_name(): None}) if isinstance(model_admin, ForUserAdminMixin): if hasattr(qs, 'for_user'): qs = qs.for_user(request.user) return [(x._get_pk_val(), smart_text(x)) for x in qs]
def get_fields_to_translatable_models(model): if model in _F2TM_CACHE: return _F2TM_CACHE[model] results = [] if NEW_META_API: for f in model._meta.get_fields(): if f.is_relation and f.related_model: # The new get_field() will find GenericForeignKey relations. # In that case the 'related_model' attribute is set to None # so it is necessary to check for this value before trying to # get translatable fields. related_model = get_model_from_relation(f) if get_translatable_fields_for_model( related_model) is not None: results.append((f.name, related_model)) else: for field_name in model._meta.get_all_field_names(): field_object, modelclass, direct, m2m = model._meta.get_field_by_name( field_name) # Direct relationship if direct and isinstance(field_object, RelatedField): if get_translatable_fields_for_model( field_object.related.parent_model) is not None: results.append( (field_name, field_object.related.parent_model)) # Reverse relationship if isinstance(field_object, RelatedObject): if get_translatable_fields_for_model( field_object.model) is not None: results.append((field_name, field_object.model)) _F2TM_CACHE[model] = dict(results) return _F2TM_CACHE[model]
def field_choices(self, field, request, model_admin): """ This method should return every available choices for the list filter, as a list of tuples. Each tuple contains the pk and the string representation of a related model instance. For more information, see RelatedFieldListFilter implementation: https://github.com/django/django/blob/1.10.5/django/contrib/admin/filters.py#L161-L223 """ model = field.remote_field.model if hasattr( field, 'remote_field') else field.related_field.model app_label = model._meta.app_label model_name = model._meta.object_name self.ajax_attrs = format_html( '{0}', flatatt({ 'data-app-label': app_label, 'data-model': model_name, 'data-ajax--url': reverse('jet:model_lookup'), 'data-queryset--lookup': self.lookup_kwarg })) 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 queryset = model._default_manager.all() return [(x._get_pk_val(), smart_text(x)) for x in queryset]
def __init__(self, request, model, field_path, *args, **kwargs): self.field_path = field_path value = kwargs.get('data').get(self.field_path) fields = get_fields_from_path(model, self.field_path) is_relation = fields[0].is_relation url = 'filter_model_autocomplete' if is_relation else 'filter_list_autocomplete' super(AutocompleteForm, self).__init__(*args, **kwargs) url = reverse( url, kwargs={ 'app': model._meta.app_label, 'model': model._meta.model_name, 'field_path': self.field_path } ) if request.GET: url = '%s?%s' % (url, request.GET.urlencode()) if is_relation: related_model = get_model_from_relation(fields[-1]) qs = related_model._default_manager.all() if value: qs = qs.filter(pk=value) self.fields[self.field_path] = forms.ModelChoiceField( queryset=qs, required=False, widget=ModelSelect2(url=url) ) else: choices = [(value, value)] if value else [] self.fields[self.field_path] = forms.ChoiceField( choices=choices, required=False, widget=ListSelect2(url=url), )
def __init__(self, field, request, params, model, model_admin, field_path): self.other_model = get_model_from_relation(field) self.rel_name = self.other_model._meta.pk.name self.changed_lookup_kwarg = '%s__%s__inhierarchy' % (field_path, self.rel_name) super(TreeRelatedFieldListFilter, self).__init__(field, request, params, model, model_admin, field_path) self.lookup_val = request.GET.get(self.changed_lookup_kwarg)
def __init__(self, field, request, params, hidden_params, model, model_admin, field_path): super(ForeignKeyFilter, self).__init__(field, request, params, hidden_params, model, model_admin, field_path) if self.filter_queryset is None: # check if there is some lookup field in field_path # and get proper QuerySet target_model = model fields = get_fields_from_path(self.model, field_path) if len(fields) > 1: target_model = get_model_from_relation(fields[-2]) self.filter_queryset = getattr(target_model, self.field.name).get_queryset() field = forms.ModelChoiceField( queryset=self.filter_queryset, required=False, empty_label='', widget=autocomplete.ModelSelect2Multiple( url=self.autocomplete_url, )) self._add_media(model_admin) attrs = self.widget_attrs.copy() attrs['id'] = 'id-%s-dal-filter' % self.field_path attrs['data-placeholder'] = self.title rendered_widget = field.widget.render(name=self._parameters[0], value=self._values[0], attrs=attrs) self.context.update({'rendered_widget': rendered_widget})
def field_choices(self, field, request, model_admin): model = field.remote_field.model if hasattr( field, 'remote_field') else field.related_field.model app_label = model._meta.app_label model_name = model._meta.object_name self.ajax_attrs = format_html( '{0}', flatatt({ 'data-app-label': app_label, 'data-model': model_name, 'data-ajax--url': reverse('jet:model_lookup'), 'data-queryset--lookup': self.lookup_kwarg })) if self.lookup_val is None: return [] 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 queryset = model._default_manager.filter(**{ rel_name: self.lookup_val }).all() return [(x._get_pk_val(), smart_text(x)) for x in queryset]
def __init__(self, field, request, params, model, model_admin, field_path): super().__init__(field, request, params, model, model_admin, field_path) if hasattr(field, 'verbose_name'): self.title = field.verbose_name else: other_model = get_model_from_relation(field) self.title = other_model._meta.verbose_name
def field_choices(self, field, request, model_admin): model = (field.remote_field.model if hasattr(field, "remote_field") else field.related_field.model) app_label = model._meta.app_label model_name = model._meta.object_name self.ajax_attrs = format_html( "{0}", flatatt({ "data-app-label": app_label, "data-model": model_name, "data-ajax--url": reverse("jet:model_lookup"), "data-queryset--lookup": self.lookup_kwarg, }), ) if self.lookup_val is None: return [] 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 queryset = model._default_manager.filter(**{ rel_name: self.lookup_val }).all() return [(x._get_pk_val(), smart_text(x)) for x in queryset]
def __init__(self, field, request, params, model, model_admin, field_path): self.other_model = get_model_from_relation(field) if field.remote_field is not None and hasattr(field.remote_field, 'get_related_field'): self.rel_name = field.remote_field.get_related_field().name else: self.rel_name = self.other_model._meta.pk.name self.changed_lookup_kwarg = '%s__%s__inhierarchy' % (field_path, self.rel_name) super(TreeRelatedFieldListFilter, self).__init__(field, request, params, model, model_admin, field_path) self.lookup_val = request.GET.get(self.changed_lookup_kwarg)
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) super().__init__(field, request, params, model, model_admin, field_path) self.lookup_choices = self.field_choices(field, request, model_admin) 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_widget(self, field, *args, **kwargs): widget = CoolAutocompleteSelect(field, None, attrs=self.get_widget_attrs( self.get_param_name(), self.params, self.widget_attrs)) other_model = get_model_from_relation(field) field = forms.ModelChoiceField( queryset=other_model.objects, widget=widget, required=False, ) self.widget = field.widget
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) self.lookup_kwarg = '%s__%s__exact' % (field_path, field.target_field.name) self.lookup_kwarg_isnull = '%s__isnull' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg) self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull) self.lookup_choices = self.field_choices(field, request, model_admin) 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) self.lookup_kwarg = "%s__%s__exact" % (field_path, field.target_field.name) self.lookup_kwarg_isnull = "%s__isnull" % field_path self.lookup_val = params.get(self.lookup_kwarg) self.lookup_val_isnull = params.get(self.lookup_kwarg_isnull) super().__init__(field, request, params, model, model_admin, field_path) self.lookup_choices = self.field_choices(field, request, model_admin) 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.empty_value_display = model_admin.get_empty_value_display()
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) self.lookup_kwarg = '%s__%s__exact' % (field_path, field.target_field.name) self.lookup_kwarg_isnull = '%s__isnull' % field_path self.lookup_val = params.get(self.lookup_kwarg) self.lookup_val_isnull = params.get(self.lookup_kwarg_isnull) super().__init__(field, request, params, model, model_admin, field_path) self.lookup_choices = self.field_choices(field, request, model_admin) 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.empty_value_display = model_admin.get_empty_value_display()
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) self.lookup_kwarg = '%s__%s__in' % (field_path, field.target_field.name) self.lookup_kwarg_isnull = '%s__isnull' % field_path lookup_vals = request.GET.get(self.lookup_kwarg) self.lookup_vals = lookup_vals.split(',') if lookup_vals else list() self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull) super(admin.RelatedFieldListFilter, self).__init__(field, request, params, model, model_admin, field_path) self.lookup_choices = self.field_choices(field, request, model_admin) 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.empty_value_display = model_admin.get_empty_value_display()
def get_fields_to_translatable_models(model): if model in _F2TM_CACHE: return _F2TM_CACHE[model] results = [] for f in model._meta.get_fields(): if f.is_relation and f.related_model: # The new get_field() will find GenericForeignKey relations. # In that case the 'related_model' attribute is set to None # so it is necessary to check for this value before trying to # get translatable fields. related_model = get_model_from_relation(f) if get_translatable_fields_for_model(related_model) is not None: results.append((f.name, related_model)) _F2TM_CACHE[model] = dict(results) return _F2TM_CACHE[model]
def __init__(self, field, request, params, model, model_admin, field_path): super(MultipleSelectionFieldListFilter, self).__init__(field, request, params, model, model_admin, field_path) if self.use_multiple_selection(model_admin): # eventually use model__field__in lookup arg to support multiple selections 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_kwarg = '%s__%s__in' % (field_path, rel_name) self.lookup_kwarg_isnull = '%s__isnull' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull, None)
def __init__(self, field, request, params, model, modeladmin, field_path): self.lookup_kwarg = '{0}__in'.format(field_path) self.lookup_kwarg_isnull = '{0}__isnull'.format(field_path) self.lookup_val = request.GET.get(self.lookup_kwarg) self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull) self.other_model = get_model_from_relation(field) opts = self.other_model._mptt_meta qs = self.other_model._default_manager if self.other_model is model: qs = qs.filter( **{ '{0}__gt'.format(opts.right_attr): F(opts.left_attr) + 1, }) self.objects = qs.order_by(opts.tree_id_attr, opts.left_attr) super(NestableFieldListFilter, self).__init__(field, request, params, model, modeladmin, field_path)
def __init__(self, field, model, field_path, **settings): other_model = get_model_from_relation(field) self.lookup_kwarg = '%s__%s__in' % (field_path, field.target_field.name) self.lookup_kwarg_isnull = '%s__in' % field_path super().__init__(field, model, field_path) if settings.get('choices'): self.lookup_choices = settings.get('choices') else: self.lookup_choices = self.field_choices(field) if settings.get('title'): self.title = settings.get('title') elif hasattr(field, 'verbose_name'): self.title = field.verbose_name.capitalize() else: self.title = other_model._meta.verbose_name.capitalize()
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) self.lookup_kwarg = '%s__%s__exact' % (field_path, field.target_field.name) self.lookup_kwarg_isnull = '%s__isnull' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg) self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull) self.field_model = field super(AdminSelect2RelatedFilterWidget, self).__init__(field, request, params, model, model_admin, field_path) self.lookup_choices = self.field_choices(field, request, model_admin) 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.empty_value_display = model_admin.get_empty_value_display() self.form = self.get_form(request)
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_kwarg = '%s__%s__exact' % (field_path, rel_name) self.lookup_kwarg_isnull = '%s__isnull' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_val_isnull = request.GET.get( self.lookup_kwarg_isnull, None) 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 = None target_field = None try: other_model = get_model_from_relation(field) except NotRelationField: pass try: target_field = field.target_field except AttributeError: pass if target_field: if not isinstance(target_field, self.allowed_fields): raise ImproperlyConfigured( f'field of Input filter must be a type of {self.allowed_fields}' ) self.lookup_kwarg = '%s__%s' % ( field_path, field.target_field.name, ) if self.lookup: self.lookup_kwarg += '__%s' % self.lookup else: if not isinstance(field, self.allowed_fields): raise ImproperlyConfigured( f'field of Input filter must be a type of {self.allowed_fields}' ) self.lookup_kwarg = '%s' % (field_path) if self.lookup: self.lookup_kwarg += '__%s' % self.lookup if not self.parameter_name: self.parameter_name = self.lookup_kwarg self.lookup_kwarg_isnull = '%s__isnull' % field_path self.lookup_val = params.get(self.lookup_kwarg) self.lookup_val_isnull = params.get(self.lookup_kwarg_isnull) super().__init__(field, request, params, model, model_admin, field_path) if hasattr(field, 'verbose_name'): self.lookup_title = field.verbose_name elif other_model: self.lookup_title = other_model._meta.verbose_name self.title = self.lookup_title self.empty_value_display = model_admin.get_empty_value_display()
def field_choices(self, field, request, model_admin): app_label = field.related_model._meta.app_label model_name = field.related_model._meta.object_name self.ajax_attrs = format_html('{}', flatatt({ 'data-app-label': app_label, 'data-model': model_name, 'data-ajax--url': reverse('jet:model_lookup'), 'data-queryset--lookup': self.lookup_kwarg })) if self.lookup_val is None: return [] 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 queryset = field.related_model._default_manager.filter(**{rel_name: self.lookup_val}).all() return [(x._get_pk_val(), smart_text(x)) for x in queryset]
def field_choices(self, field, request, model_admin: ModelAdmin): model = field.remote_field.model if hasattr( field, 'remote_field') else field.related_field.model app_label = model._meta.app_label model_name = model._meta.object_name self.ajax_attrs = format_html( '{0}', flatatt({ 'data-app-label': app_label, 'data-model': model_name, 'data-ajax--url': self.url, 'data-queryset--lookup': self.lookup_kwarg, 'data-queryset--params': json.dumps(dict(request.GET.items())), 'data-filter': f"By {self.title}", **format_widget_data(self.widget_data), })) if self.lookup_val is None: return [] 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 queryset = model._default_manager.filter(**{ rel_name: self.lookup_val }).all() return [(x.pk, smart_str(x)) for x in queryset]
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) self.lookup_kwarg = '%s__%s__exact' % (field_path, field.target_field.name) self.lookup_kwarg_isnull = '%s__isnull' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg) self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull)
if not content_type or content_type not in get_host_content_types(): return publish_host_update(instance.object) api_post_create.connect(custom_field_change) api_post_update.connect(custom_field_change) # trigger publish_host_update when related model change for model_path in [ 'configuration_path', 'configuration_path__module', 'ethernet_set', 'ethernet_set__ipaddress', ]: field = get_field_by_relation_path(BaseObject, model_path) model = get_model_from_relation(field) logger.debug('Setting up handler for {} change of BaseObject'.format( model.__name__, )) post_commit( # use wraps for keep magic attributes of func like __name__ wraps(publish_host_update_from_related_model)( partial( publish_host_update_from_related_model, field_path=model_path ) ), model )
"""
def __init__(self, field, request, params, model, model_admin, field_path): super().__init__(field, request, params, model, model_admin, field_path) self.field_model = get_model_from_relation(self.field)
def choices_list(self): model = get_model_from_relation(self.field) queryset = model._default_manager.all() return [(i._get_pk_val(), self.label_for_instance(i)) for i in queryset]