문제

So I am using Djangos builtin comments app (django.contrib.comments) for my blog application. I made several changes to the forms display, etc. (as described in the documentation). One last thing that bothers me is that the Comments model is in it's own category in the admin, like this:

MyApp
---Model1
---Modle2

Comments
---Comments

I want it to be like this since the comments are tied to the MyApp models.

MyApp
---Model1
---Modle2
---Comments

I tried to achieve this by adding this line of code to MyApps admin.py (overwriting the Comment class)

class MyAppComment(Comment):

    class Meta(Comment.Meta):
       app_label = 'myapp'

admin.site.unregister(Comment)
admin.site.register(MyAppComment, CommentsAdmin)

And this works (and the Comments model shows up under MyApps) but now the links are wrong...the model points to:

http://www.mysite.com/admin/myapp/myappcomment/

which outputs an error:

no such table: myapp_myappcomment

instead of:

http://www.mysite.com/admin/comments/comment/

This is because the admin forms it's urls according to app names and model names...how could I just change the position of the Comments model in the admin but leave the urls as they are?

There must be some way to do it?

도움이 되었습니까?

해결책

You haven't 'overwritten' the Comment class -- by subclassing it, you've actually created a child model using multi table inheritance. This is why another table needs to be created.

You could create a proxy model that inherits from the Comment class, then no additional tables need to be created.

class MyAppComment(Comment):

    class Meta(Comment.Meta):
        proxy = True

admin.site.unregister(Comment)
admin.site.register(MyAppComment, CommentsAdmin)

You shouldn't need to set app_label if MyAppComment is defined in the myapp app - it will be set automatically.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top