I am new to Django I am using autocomplete_fields
, Here is the Case. I have an app named Contacts
where I am getting contact information and there is another app named school where I am Enrolling Contacts as person from my Contact app
here is models.py
class FacultyRegistration(models.Model):
role = models.ForeignKey(Roles, on_delete=models.CASCADE, null=True, verbose_name="Faculty Role")
contact = models.ForeignKey('Contacts.Contact', related_name='faculty_Contact', on_delete=models.CASCADE)
reference = models.ForeignKey('Contacts.Contact', on_delete=models.CASCADE, related_name='faculty_reference', )
Here I am registering person and using my Contact app for it
#admin.py
@admin.register(FacultyRegistration)
class PersonRegistrationAdmin(admin.ModelAdmin):
list_display = ('id', 'role', 'contact', 'reference')
list_filter = ('role',)
list_display_links = ('id', 'role', 'contact', 'reference', 'contact',)
autocomplete_fields = ["role", 'contact', 'reference']
search_fields = ['role__person_role', 'contact__first_name', 'contact__middle_name', 'contact__last_name', 'reference__first_name', 'reference__middle_name', 'reference__last_name']
the issue is when I go to my admin panel I get searchable drop downs as expected
but when I start typing to search my contact it wont fetch contacts
and it gives this error in console
django.core.exceptions.FieldError: Unsupported lookup ‘icontains’ for ForeignKey or join on the field not permitted.
[22/Jan/2024 12:33:08] “GET /autocomplete/?term=stu&app_label=school_app&model_name=facultyregistration&field_name=contact HTTP/1.1” 500 191958
however it works fine with my roles, maybe bcz roles are in same application