OneToOne关系和django-autocomplete-light

时间:2015-11-20 18:11:04

标签: python django django-orm one-to-one django-autocomplete-light

我的django-autocomplete-light有些问题我无法自行解决。

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

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

autocomplete_light.register(UserProfileAutocomplete)

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

我已经尝试了“用户”,但它引发了:TypeError,相关字段无效查找:icontains

我该怎么办?

非常感谢。

1 个答案:

答案 0 :(得分:1)

您需要用户模型的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)