Lec. 1, Admin Site--CollectionAdmin

Hello all,

I am running into an issue that I cannot figure out. I am not getting errors per se when coding:

@admin.display(ordering=‘products_count’)

def products_count(self,collection):

    url = ( reverse('admin:store_product_changelist')

            +'?'

            + urlencode({

               'collection__id': str(collection.id)

            }))

    return format_html('<a href="{}">{}</a>', url,collection.products_count)

   

def get_queryset(self, request):

    return super().get_queryset(request).annotate(

        products_count= count('product') )

the code executes with code 200, but the changes do not show in the server.

I think this is linked to a new challenge that I am running into.

When I code to solve the autocomplete_fields = [‘collection’], the “ERRORS:
<class ‘store.admin.ProductAdmin’>: (admin.E040) ModelAdmin must define “search_fields”, because it’s referenced by ProductAdmin.autocomplete_fields” and then add “search_fields = [‘title’]” to CollectionAdmin it does not recognize the update and throws the same error.

Any help/suggestion would be greatly appreciated. Thanks, in advance.

I got the same error almost, but it showed up after chapt 13. Editing children using inlines.

My error shows like this:

Traceback (most recent call last):
File “C:\Python38\lib\threading.py”, line 932, in _bootstrap_inner
self.run()
File “C:\Python38\lib\threading.py”, line 870, in run
self._target(*self._args, **self._kwargs)
File “D:\projects\PythonRelated\Django-mosh\storefront\lib\site-packages\django\utils\autoreload.py”, line 64, in wrapper
fn(*args, **kwargs)
File “D:\projects\PythonRelated\Django-mosh\storefront\lib\site-packages\django\core\management\commands\runserver.py”, line 134, in inner_run
self.check(display_num_errors=True)
File “D:\projects\PythonRelated\Django-mosh\storefront\lib\site-packages\django\core\management\base.py”, line 558, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
<class ‘store.admin.OrderItemInline’>: (admin.E040) ProductAdmin must define “search_fields”, because it’s referenced by OrderItemInline.autocomplete_fields.

Let me know if you discuss the mystery error and solution :slight_smile:

1 Like

I’m also in 13 - Editing Children Using Inlines and figured it out!

As per the error message jazzthedog got, I went to the ProductAdmin class and added the search fields:

class ProductAdmin(admin.ModelAdmin):
    search_fields = ['title']
    ...

and we’re back in business!