2

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.

Moe Far
  • 2,742
  • 2
  • 23
  • 41
Zoli
  • 831
  • 1
  • 11
  • 27
  • Is there more information after "icontains" ? Field errors = can't find the field you're trying to reference, Type Errors = invalid format match (try examining the data type you're using in the DB, how you initialized it in your models.py, and the mismatch etc.) – jdero Nov 20 '15 at 18:53
  • @MoeFar Please stop make one-off tag edits to posts. Single tag edits, especially if they don't improve on the formatting or content of the post are heavily frowned upon here. It takes three people to approve your edits, and these edits are so minor, it quite literally wastes our time. – Zizouz212 Nov 22 '15 at 15:47

1 Answers1

1

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)
doniyor
  • 36,596
  • 57
  • 175
  • 260