I am not able to add auto-complete for the OneToOneField in Django admin using django-autocomplete-light. This field is available in Django Rest Framework's auth_token model.
class Token(models.Model):
"""
The default authorization token model.
"""
key = models.CharField(_("Key"), max_length=40, primary_key=True)
user = models.OneToOneField(
settings.AUTH_USER_MODEL, related_name='auth_token',
on_delete=models.CASCADE, verbose_name=_("User")
)
created = models.DateTimeField(_("Created"), auto_now_add=True)
models.py
from rest_framework.authtoken.models import Token
class CustomAuthTokenModel(Token):
search_fields = ['user', ]
def __str__(self):
return str(self.user)
views.py
from dal import autocomplete
from rest_framework.authtoken.models import Token
class AuthUserAutocompleteView(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = Token.objects.all().order_by('user__username')
if self.q:
qs = qs.filter(user__username__icontains=self.q)
return qs
admin.py
from django.contrib import admin
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.admin import TokenAdmin
from .forms import AuthUserForm
class CustomTokenAdmin(TokenAdmin):
form = AuthUserForm
admin.site.unregister(Token)
admin.site.register(Token, CustomTokenAdmin)
forms.py
from dal import autocomplete
from .models import CustomAuthTokenModel
class AuthUserForm(autocomplete.FutureModelForm):
class Meta:
model = CustomAuthTokenModel
fields = ('user', )
search_fields = ['user__username']
widgets = {
'user': autocomplete.ModelSelect2(url='autocomplete_auth_user')
}
Is there a issue from my side, there is an issue in dal. Tried a few other things, did not resolve the issue. How would you add auto-complete when you do not model is being exactly used. Currently it shows me the entry in terms of keys.
I am not able to add auto-complete for the OneToOneField in Django admin using django-autocomplete-light. This field is available in Django Rest Framework's auth_token model.
models.py
views.py
admin.py
forms.py
urls.py has
Is there a issue from my side, there is an issue in dal. Tried a few other things, did not resolve the issue. How would you add auto-complete when you do not model is being exactly used. Currently it shows me the entry in terms of
keys
.