I have a user, admin and employee models, both employee and admin are related to the user model with a oneToOne field what i would like to do is in the admin form i have one field "user"
instead of having a drop down field with all users i would like to have a search box so that when i search for a specific user i find him and choose him to be an admin.
how?
i also tried to add user's fields to admin's form but i couldn't
i tried the inline thing, the parent_link in the admin's model... but i couldn't find a solution
Now i would like to minimise the search through the long users list by adding a search box to the field
Asked
Active
Viewed 780 times
3

leila
- 461
- 1
- 7
- 21
2 Answers
1
I have solved this type of problem using django-ajax-selects package. Hope this will help you.
app/admin.py:
from ajax_select import register, LookupChannel
@register('users')
class UsersLookup(LookupChannel):
model = User
def get_query(self, q, request):
return self.model.objects.filter(username__icontains=q)
class EmployeeAdminForm(forms.ModelForm):
user = AutoCompleteSelectField('users', required=False,
help_text=None)
class Meta:
model = Employee
fields = '__all__'
settings.py
AJAX_LOOKUP_CHANNELS = {
'users' : {'model': 'auth.user', 'search_field': 'username'},
}
-
thank you for ur answer but m getting "Reverse for 'ajax_lookup' not found. 'ajax_lookup' is not a valid view function or pattern name."???? – leila Aug 23 '17 at 01:25
-
You have to add ' url(r'^ajax_select/', include(ajax_select_urls)),' in your urls.py check the installation page. :) – Athena Aug 23 '17 at 02:04
-
if you can check this https://stackoverflow.com/questions/45786983/add-fields-to-model-from-another-one-related-with-onetoonefield-or-foreignkey-re – leila Aug 23 '17 at 02:15
0
Use django's raw_id_fields. With that you can declare a foreign key object searchable.

Max M
- 472
- 4
- 12