F
F
FFSinit2015-05-06 12:54:32
Django
FFSinit, 2015-05-06 12:54:32

How to configure django_autocomplete_light to work with GenericForeignKey?

So the essence of the problem, there is a model Foo

class Foo(Displayable):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

And there is a Bar model
class Bar(models.Model):
    name = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255)

There will be a few more similar Bar models in the future, and we need to be able to associate a Foo instance with one of the Bar or similar model instances. Therefore, it was decided to use GenericForeignKey . By default, it is very inconvenient to create such links in the junga admin panel, so autocomplete_light was googled, which, judging by the documentation, makes it very convenient.
I seem to have done everything as expected, in the root of the application with the admin panel of which we are working, an autocomplete_light_registry.py file has been created with the following content:
import autocomplete_light
from barapp.models import Bar


class AutocompleteBarItems(autocomplete_light.AutocompleteGenericBase):
    choices = (
       Bar.objects.all(),
    )

    search_fields = (
        ('name', ),
    )

autocomplete_light.register(AutocompleteModelItems)

In admin.py
class EntryAdmin(admin.ModelAdmin):
    form = autocomplete_light.modelform_factory(Entry)

As a result I get
KeyError: u'manager'
instead of a working autocomplete.
Tell me who can, I already broke my whole brain with this auto-complete.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FFSinit, 2015-05-06
@FFSinit

he asked, he answered. The problem is in django 1.7 which I forgot to mention in the question. Works with 1.5.8.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question