Search code examples
djangodjango-admindjango-extensions

ForeignKeyAutocompleteAdmin


Given these two models:

class Product(Model):
    upc = CharField(max_length=96, unique=True)
    ...
    class Meta:
        app_label = 'scrapers'

class Order(Model):
    ...
    product = ForeignKey('scrapers.Product', related_name='orders', on_delete=models.CASCADE)
    objects = OrderManager()

    class Meta:
        app_label = 'scrapers'

And this admin.py:

class OrderAdmin(ForeignKeyAutocompleteAdmin):
    related_search_fields = {
        'product': ('upc', 'retailer_offerings__name')
    }
    fields = ('product', 'client', 'completed', 'expires', 'amount', 'filled')


admin.site.register(Order, OrderAdmin)

Having done collectstatic and declared django_extensions and my app in INSTALLED_APPS. Why am I getting this:

[04/Dec/2016 05:54:28] "GET /admin/scrapers/product/foreignkey_autocomplete/?search_fields=upc&app_label=scrapers&model_name=product&q=045496 HTTP/1.1" 302 0
Not Found: /admin/scrapers/product/foreignkey_autocomplete/change/
[04/Dec/2016 05:54:28] "GET /admin/scrapers/product/foreignkey_autocomplete/change/ HTTP/1.1" 404 1875

On input to the input field (box to the left not pk input to the right)?

The Product table has millions of rows and the default admin configuration doesn't handle this well so I tried the extensions package solution. The widget is requesting product/foreignkey_autocomplete but a quick grep through manage.py show_urls shows that only /orders has been registered with the foreignkeyautocomplete package. I don't see anything in the docs addressing url configuration (I assume this is done when registering with the admin). How can I get this to work?

Partial solution:

after examining the urls and realizing it was attempting top send the search queries to /product/foreignkey_autocomplete/... I tried to create an empty admin for that model as well. It worked but the behavior is still odd. It seems to stop searching after 4-5 characters have been entered and doesn't bother refreshing.


Solution

  • as per my update, adding a ForeignKeyAutocompleteAdmin for the other side of the relation created the missing urls and the functionality seems to work