Search code examples
python-3.xdjango-admindecoratordjango-1.8

Recording classes in Django Admin with @admin.register decorator


I am testing the new @admin.register decorator that is a new feature from Django 1.7. I am currently using Django 1.8.2 and Python 3 and happened me the following situation in relation to @admin.register decorator:

In my admin.py file I have:

from django.contrib import admin
from .models import Track

# Register your models here.
@admin.register(Track)
class TrackAdmin(admin.ModelAdmin):
    list_display = ('title','artist')

And when I try http://localhost:8000/admin/ I get the following output in my browser:

AttributeError

Now, When I use the admin.site.register(Track,TrackAdmin)that is the traditional way of register models and classes in the django admin, happened me the same

from django.contrib import admin
from .models import Track


# Register your models here.

class TrackAdmin(admin.ModelAdmin):
    list_display = ('title','artist')

admin.site.register(Track,TrackAdmin)

How to can I use the @admin.register decorator for record together classes? (Track and TrackAdmin)

Thanks a lot. :)


Solution

  • Please see following reference: https://docs.djangoproject.com/en/1.8/ref/contrib/admin/

    Place your decorator @admin.register(Track) before your class as a wrapper for it.

    from django.contrib import admin
    from .models import Track
    
    # Register your models here
    
    @admin.register(Track)
    class TrackAdmin(admin.ModelAdmin):
        list_display = ('title','artist')