OneToOne关系和django-autocomplete-light

时间:2022-06-15 19:37:05

I have some problems with the django-autocomplete-light which I cannot resolve on my own.

我对django-autocomplete-light有一些问题,我自己无法解决。

models.py

models.py

from django.contrib.auth.models import User
class UserProfile(models.Model):    
   user = models.OneToOneField(User, related_name="user_profile")
   ...

autocomplete_light_registry.py

autocomplete_light_registry.py

class UserProfileAutocomplete(autocomplete_light.AutocompleteModelBase):
    model = UserProfile
    search_fields = ['username']
    attrs = {
        'data-autocomplete-minimum-characters': 1,
    }

autocomplete_light.register(UserProfileAutocomplete)

I tried this but it raises an error: FieldError, Cannot resolve keyword u'username' into field.

我试过了,但它引发了一个错误:FieldError,无法将关键字u'username'解析到字段中。

I've already tried "user" but it raises: TypeError, Related Field got invalid lookup: icontains

我已经尝试过“user”,但它会引发:TypeError,相关字段有无效查找:icontain

What should I do?

我应该做什么?

Thanks a lot.

非常感谢。

1 个解决方案

#1


1  

you need User model's username. so you need user__username

您需要用户模型的用户名。所以你需要user__username

class UserProfileAutocomplete(autocomplete_light.AutocompleteModelBase):
    model = UserProfile
    search_fields = ['user__username'] # <-- user__username instead username
    attrs = {
       'data-autocomplete-minimum-characters': 1,
    }

    autocomplete_light.register(UserProfileAutocomplete)

#1


1  

you need User model's username. so you need user__username

您需要用户模型的用户名。所以你需要user__username

class UserProfileAutocomplete(autocomplete_light.AutocompleteModelBase):
    model = UserProfile
    search_fields = ['user__username'] # <-- user__username instead username
    attrs = {
       'data-autocomplete-minimum-characters': 1,
    }

    autocomplete_light.register(UserProfileAutocomplete)