Search code examples
pythondjangodjango-modelsdjango-formsdjango-autocomplete-light

FieldError at /autocomplete/ItemsAutocomplete/


I am using autocomplete_light in a django model form.

this is my model for the form

class Ca_dispensaries_item(TimeStampedModel):
  item = models.ForeignKey(Items)
  dispensary = models.ForeignKey(Ca_dispensaries)
  description = models.CharField(max_length=5000, null=True)

this is the form

class CamenuForm(autocomplete_light.ModelForm):
   class Meta:
     model = Ca_dispensaries_item
     exclude = ('dispensary',)
     autocomplete_fields = ('item',)

registered as

autocomplete_light.register(Items, search_fields=('item_name'))

when i try to enter some values in the item, as per the autocomplete feature, it starts searching but gives field error Cannot resolve keyword u'i' into field. Choices are: arizona_dispensaries_item, ca_dispensaries_item, colorado_dispensaries_item, created, id i dont know from where this i is coming from. also, dispensaries_items are some of the models. while created and id are field names


Solution

  • You have forgottent a comma! Change

    search_fields=('item_name')

    to

    search_fields=('item_name',)

    search_fields should be an iterable, so if it has the value ('item_name') (which is a string) it will get 'i', 't', 'e', etc (that's why you get the message Cannot resolve keyword u'i' into field)

    Also, there are some serious problems with the names your models: I see that you have a model named Items and a model named Ca_dispensaries_item. You should not name your models in plural, so Items should be Item and you need to use CamelCase with class names, so Ca_dispensaries_item should be CaDispensariesItem``.