Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My admin.TabularInline class returns exception: object has no attribute 'urls'

So I've been Googling around and can't find a solution to my problem. I'm honestly quite puzzled, so thanks for taking a look.

mysite/mysite/urls.py:

...
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
                         url(r'^admin/', include(admin.site.urls)),
...

mysite/upgradelists/admin.py:

from django.contrib import admin
from upgrademe.models import GPU, CPU

class CPUAdmin(admin.TabularInline):
    model = CPU

admin.site.register(CPU, CPUAdmin)

returned error:

AttributeError at /admin/
'CPUAdmin' object has no attribute 'urls'

However, if I change admin.py to:

class CPUAdmin(admin.ModelAdmin):
    model = CPU

Then all is well. (although, irrelevant note: I believe the 'model = CPU' portion is redundant?)

Any help/insight into this would be greatly appreciated. Google has left me stumped, and searches over StackOverflow have turned up nothing that I can see is related.

like image 705
aljovidu Avatar asked Feb 13 '14 19:02

aljovidu


4 Answers

I had the same problem, Google led me to this thread and it didn't help. I solved it while I was about to post my question.

I don't even know if it's the same problem you had, but here it is:

class UserAnswerInline(admin.TabularInline):
    model = UserAnswer
class UserQuestionAdmin(admin.ModelAdmin):
    inlines = [UserAnswerInline]
admin.site.register(UserQuestion, UserAnswerInline)

The correct code:

class UserAnswerInline(admin.TabularInline):
    model = UserAnswer
class UserQuestionAdmin(admin.ModelAdmin):
    inlines = [UserAnswerInline]
admin.site.register(UserQuestion, UserQuestionAdmin)

Spot the difference? Yup, wrong class name. Took me 5 hours before I decided to create a new question here, then figured it out while explaining the problem.

like image 66
davidtgq Avatar answered Nov 03 '22 00:11

davidtgq


You can't register a tabular admin class directly with the admin site. TabularAdmin is a subclass of InlineAdmin, and as such is only for use in the inlines attribute of a full ModelAdmin.

like image 40
Daniel Roseman Avatar answered Nov 03 '22 01:11

Daniel Roseman


I have the same problem like this.

You can try this to fix this problem:

class CPUInline(admin.TabularInline):
    model = CPU

class CPUAdmin(admin.ModelAdmin):
    inlines = [CPUInline]

admin.site.register(CPU, CPUAdmin)
like image 37
Fad Lee Avatar answered Nov 03 '22 00:11

Fad Lee


This error happens to be coming from the part you expect least.

You can’t register InlineModelAdmin or it’s subclasses in the admin site. You can only use them in attributes of ModelAdmin classes.

This is the the most common cause of such error and it’s quite difficult to spot

like image 35
Trevor Avatar answered Nov 03 '22 00:11

Trevor