#Abstract Class Admin Settings

26 messages ยท Page 1 of 1 (latest)

viral spruce
#

I have an abstract class for automatically adding timestamps to my models that have auto_now or auto_now_add. Can I patch Django admin so they show up, register some setting, or do something else so that all models that inherit from them will have those fields show up in a read only way in the admin?

frail cedar
#

I don't think you need to do anything special to have those fields show up.

viral spruce
frail cedar
#

ah ok that has nothing to do with the base class, then

#

just add the field name to the list_display attribute in the admin class

#

class Base(models.Model):
    created_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

class Question(Base):
    question_text = models.CharField(max_length=200, db_comment="I hold questions")
...
viral spruce
#

I can't create admin settings for my version of Base

frail cedar
#
class QuestionAdmin(admin.ModelAdmin):
...
    list_display = [
        "question_text",
        "created_at",
``` ...
frail cedar
viral spruce
#

I can't do

@admin.register(Base)
class BaseAdmin(admin.ModelAdmin):
  ...

or any variation of this

frail cedar
#

what do you mean by "can't"? Is your keyboard broken? ๐Ÿ™‚

viral spruce
#

Python throws an error ๐Ÿ™‚

frail cedar
#

why not post the error message, and maybe I can figure out what's wrong

viral spruce
#

django.core.exceptions.ImproperlyConfigured: The model TimestampedModel is abstract, so it cannot be registered with admin.

#
class TimestampedModel(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True
frail cedar
#

ah, I don't use abstract models, so I don't know ๐Ÿ˜

#

looks like adding the field to the admins for the child classes will work though.

viral spruce
#

Yea but then I have to add it to everyone

#

codegen probably is the fastest way to do this though without patching Django itself

frail cedar
#

well you can always make a base admin class that adds that to the list_display, and then all the other admin classes inherit from it

viral spruce
frail cedar
#

it's pretty complicated ๐Ÿ˜•

viral spruce
#

I've slogged through worse lmao

frail cedar
#

hm now I'm trying that inheritance thing and cannot figure it out ๐Ÿ˜•

#

well, it works, but it still requires that you edit every child class ๐Ÿ˜ฆ