Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin model Inheritance is it possible?

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+

like image 859
Gautam Avatar asked Sep 11 '11 03:09

Gautam


People also ask

Can we inherit models in Django?

Model Inheritance in Django works almost identically to the way normal class inheritance works in python. In this article we will revolve around how to create abstract base class in Django Models. Abstract Base Class are useful when you want to put some common information into a number of other models.

Does Django support multiple inheritance?

This style is used, if you only want to modify the Python level behaviour of the model, without changing the model's fields. You Inherit from base class and you can add your own properties except fields. base class should not be abstract class. we can not use multiple inheritance in proxy models.

How many types of inheritance are there in Django?

There are five types of inheritances: Single Inheritance. Multiple Inheritance. Multilevel Inheritance.

Is Django admin useful?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.


2 Answers

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])
like image 142
Kokos Avatar answered Sep 21 '22 18:09

Kokos


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.

like image 32
Etienne Avatar answered Sep 18 '22 18:09

Etienne