简体   繁体   中英

OneToOne relationship and django-autocomplete-light

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

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)

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

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

What should I do?

Thanks a lot.

you need User model's username . so you need 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM