Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. :)

like image 315
bgarcial Avatar asked Jul 06 '15 00:07

bgarcial


People also ask

What is Fieldsets in Django admin?

fieldsets is a list of two-tuples, in which each two-tuple represents a <fieldset> on the admin form page. (A <fieldset> is a “section” of the form.)

Is Django admin good for production?

Django's Admin is amazing. A built-in and fully functional interface that quickly gets in and allows data entry is priceless. Developers can focus on building additional functionality instead of creating dummy interfaces to interact with the database.


1 Answers

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

Place your decorator @admin.register(Track) before your class as "sugar" or 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')

like image 134
Alberto Millan Avatar answered Sep 20 '22 11:09

Alberto Millan