Django admin model Inheritance is it possible?
Asked Answered
H

6

7

Is inheritance possible in admin Models ?

Like For Example consider the following ,

File : models.py

class AbstractModel ( models.Model ):
    # Meta Information common to all classes
    author = models.ForeignKey(auth.models.User , null = False ,related_name="%(class)s_related_author" ) # The user who created 
    editor = models.ForeignKey(auth.models.User , null = True,related_name="%(class)s_related_editor" ) # The user who last edited                   
    created_at = models.DateTimeField(auto_now_add  = True) # Create Time
    edited_at = models.DateTimeField(auto_now = True) # Modify Time

    class Meta:
                abstract = True


class Topic( AbstractModel ):
    name = models.CharField(max_length = NameMaxLength , unique = True)
    version_number = models.IntegerField(default = 0)
    update_frequency = models.IntegerField()

A similar inheritance does not seem to produce the correct result when used in ModelAdmin

File : admin.py

class Abstract_Admin_Model( admin.ModelAdmin ):
        fields =  ('author' , 'editor' , 'created_at' , 'edited_at')
        readonly_fields = ('author' , 'editor' , 'created_at' , 'edited_at')

        def save_model(self, request, obj, form, change):
                if not change :
                        obj.author = request.user
                else : 
                        obj.editor = request.user
                obj.save()

class Admin_Topic( Abstract_Admin_Model ):
     fields += ('name' , 'version_number' , 'update_frequency')


admin.site.register( Topic , Admin_Topic )

EDIT:

I've modified the above model based on suggestions ,

If the admin.py is like so , I don't get any error , and the model appears on the admin.

class AbstractAdminModel(  admin.ModelAdmin  ):
        pass#fields = ['author' , 'editor' , 'created_at' , 'edited_at']


class Admin_Topic( AbstractAdminModel ):
    pass

admin.site.register( Topic , Admin_Topic )

But If i modify it like so

class AbstractAdminModel(  admin.ModelAdmin  ):
    fields = ['author' , 'editor' , 'created_at' , 'edited_at']


class Admin_Topic( AbstractAdminModel ):
    pass

admin.site.register( Topic , Admin_Topic )

I get the following error :

the Error

Here is a stack trace Link

Problem : The model does not even appear on the Admin Page

Extra Info:

using django 1.2.5 with pinax 0.7.2 , Ubuntu 11.04 , python 2.7.1+

Huda answered 11/9, 2011 at 3:15 Comment(0)
N
8

Maybe it is bit to late for you for the answer, but I think others can have similar problem - as I did.

Here is my solution - I am not sure if it is proper, but it works for me and non other from above can do the same (assuming that you want a multitable inheritance (non abstract model), as I do)

class SiteEntityAdmin(admin.ModelAdmin):
    fieldsets = [
            (None, {'fields': ['name']}),
    ]


class PhotoAdmin(SiteEntityAdmin):
    fieldsets = [
             ('Photo details', {'fields': ['photo_url', 'description']}),
    ]
    fieldsets.insert(0, SiteEntityAdmin.fieldsets[0])
Nicolettenicoli answered 19/11, 2011 at 21:11 Comment(0)
W
4

Yes it's possible. I think the error you done is to put:

class Meta:
    abstract = True

in your Abstract_Admin_Model class. Try without the Meta class.

Weigle answered 11/9, 2011 at 3:33 Comment(9)
It did not make any difference actuallyHuda
Are you sure you have your APP in INSTALLED_APPS in settings.py? Does it work if try your Admin_Topic without inheritance?Weigle
I am Quite sure that the App is installed be cause It worked before I created the Abstract Class and tried to inherit itHuda
Just a guess, maybe the problem is with the fields += .... Try something along those lines: fields = Abstract_Admin_Model.fields + (...).Weigle
You know, admin.ModelAdmin are just a normal python class (not like Model class that have a lot of Meta works in it), so normal inheritance is working normally (without any use of Meta class). If it doesn't work for you, the problem is not in the inheritance (you already inherit from admin.ModelAdmin!) so you need to check for it elsewhere.Weigle
Look at this question: #1033943. It will explain your error mention in my second comment.Weigle
Nope That hasn't really helped me . I'll modify the question to showcase .Huda
I did some tests on my side and I can't reproduce your bug (but I'm with Django 1.3). I can help you more if you show me your real code (Admin_RSSFeed and al.). In the mean time, something you could try is to set fields only in your child class (Admin_Topic) and check the results. By the stacktrace it look that the created_at field was not created in the form and the validation look for this field? It could be something related to pinax also.Weigle
I'll try out django 1.3 , and get back . I'll also check if i can release the sourceHuda
M
3

To use the parent's class attributes, such as list_display or search_fields you can do the following:

@admin.register(BaseClass)
class BaseClassAdmin(admin.ModelAdmin):
    list_display = ('field_a', 'field_b')
    search_fields = ('field_a', 'field_b')

@admin.register(ChildClass)
class ChildClassAdmin(BaseClassAdmin):
    def get_list_display(self, request):
        return self.list_display + ('field_c', 'field_d')

    def get_search_fields(self, request):
        return self.search_fields + ('field_c', 'field_d')

Similarly you can do that for other attributes like actions, readonly_fields, etc.

Monastic answered 31/10, 2019 at 16:52 Comment(0)
P
2

The problem is here:

class Admin_Topic( admin.ModelAdmin ):

This line controls the inheritance, so it should be:

class Admin_Topic( Abstract_Admin_Model ):

Also worth noting: you may wish to use TopicAdmin rather than Admin_Topic to better match the Django convention.

Primary answered 11/9, 2011 at 3:38 Comment(3)
Sorry , For the accidental mistake in the code , I have edited the code , It doesn't work even thenHuda
Sorry for being pedantic, but did you restart the web service after making the change?Primary
:-) , I did , The web service was running while i was editing , I also tried restarting itHuda
H
0

Try changing:

    class Meta:
        abstract = True

to

    class Meta:
        model = Topic
        abstract = True
Honourable answered 11/9, 2011 at 4:10 Comment(0)
N
0

The inheritance in your modified admin.py works. The problem is that you are adding the field 'created_at' to the admin (Admin_RSSFeed), but it does not exist on the model (probably named RSSFeed?). (At least that is what the error screenshot tries to tell you.)

Newsreel answered 7/4, 2014 at 4:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.