Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict foreign key objects for autocomplete search results in django ModelAdmin.autocomplete_fields?

Following this answer, I was able to filter foreign keys choices to select:

enter image description here

But when I mark spm as autocomplete_field:autocomplete_fields = ['spm'], the spm field becomes from select field to autocomplete search field: enter image description here

But the foreign key choices are not restricted as configured in "formfield_for_foreignkey" any more.

Even when I am attaching the widget inside formfield_for_foreignkey method, spm autocomplete options are getting restricted:

@admin.register(CustomModel)
class CustomModelAdmin(admin.ModelAdmin):

    #autocomplete_fields = ['spm']
    search_fields = ['name']

    def get_form(self, request, obj=None, **kwargs):
        request.current_object = obj
        return super(CustomModelAdmin, self).get_form(request, obj, **kwargs)

    def formfield_for_foreignkey(self, db_field, request, **kwargs):

        if db_field.name == 'spm':
            instance = request.current_object
            if instance.brand and instance.memory_size:
                    filtered_qs=StandardProductWithMemorySize.objects.filter(
                        product__brand=instance.brand,
                        memory_size=instance.memory_size
                    )
                    kwargs['queryset'] = filtered_qs
                    db = kwargs.get('using')
                    kwargs['widget'] = AutocompleteSelect(db_field.remote_field, self.admin_site)
        return super(
            CustomModelAdmin, self
        ).formfield_for_foreignkey(db_field, request, **kwargs)
like image 646
javed Avatar asked Feb 04 '18 08:02

javed


2 Answers

Instead of using autocomplete_fields = ['spm'], overriding change_form.html template and using JavaScript to make HTML select element searchable(with auto-completion) worked for me:

enter image description here

content of change_form.html:

{% extends 'admin/change_form.html' %}

{% block admin_change_form_document_ready %}
    {{ block.super }}
    <script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.min.js"></script>
    <script type="text/javascript">
            $("#id_spm").select2({});
    </script>

{% endblock %}

The HTML select element on which select2 function operate:

<select name="spm" required="" id="id_spm" >
  <option value="" selected="">---------</option>
  <option value="67688">apple iphone 7</option>
  <option value="69093">apple iphone 7 plus</option>
  <option value="71453">apple ipad pro</option>
  <option value="71076">apple ipad pro 9.7</option>
  <option value="34840">apple ipad pro 10.5</option>
  <option value="72303">apple iphone 8 plus</option>
  <option value="72301">apple iphone 8</option>
  <option value="72307">apple iphone x</option>
  <option value="71243">apple ipad pro 12.9</option>
</select>
like image 173
javed Avatar answered Oct 23 '22 22:10

javed


This is a bug in Django, unfortunately still unresolved at the time of this writing: https://code.djangoproject.com/ticket/29707

like image 1
Carsten Fuchs Avatar answered Oct 23 '22 23:10

Carsten Fuchs