Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

django - 在Django 2.2中的admin.py中注册抽象类的派生子类(Registering derived child class of abstract class in admin.py in django 2.2)

Does anyone know, how to register child class derived from abstract class in admin.py (the abstract class is in file abstract_models.py file) I tried major solution in the web but does not seem to work.

(有谁知道,如何在admin.py中注册从抽象类派生的子类 (抽象类在文件abstract_models.py文件中),我在网络上尝试了主要解决方案,但似乎不起作用。)

It is possible as pointed by various contributors but don't know what I am doing wrong!

(正如各种贡献者所指出的那样,这是可能的,但不知道我在做什么错!)

My folder structure is like this

(我的文件夹结构是这样的)

'''

(''')

gaasc/ <- project folder

     contain urls.py, admin.py,models.py,etc

gaasc_apps/<- contains all apps

    core/
       abstract_models.py
       models.py
       admin.py,...

    about_app/
      models.py, 
      admin.py
      urls.py

'''

(''')

I am trying to leverage abstract class inside core app to models inside about_app app .

(我正在尝试利用核心应用程序内的抽象类到about_app应用 程序内的模型。)

Yes, between different apps.

(是的,在不同的应用程序之间。)

Steps followed:

(遵循的步骤:)

  1. create abstract_models.py and define abstract class

    (创建abstract_models.py并定义抽象类)

  2. import it in core/models.py

    (将其导入core / models.py)

3.import the abstract class from core/models.py inside about_app/models.py

(3,从about_app / models.py的core / models.py导入抽象类)

  1. register the class in about_app/models.py to admin.py(in about_app/)

    (在about_app / models.py中将类注册为admin.py(在about_app /中))

abstract_models.py file(inside core/abstract_models.py) has

(abstract_models.py文件(在core / abstract_models.py内部)具有)

import uuid
from django.db import models


class AbstractTimeStampModel(models.Model):
     """TimeStampModel that holds created_date and updated_date       field"""
     id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
     created_date = models.DateTimeField("Created date", auto_now_add=True)
     updated_date = models.DateTimeField("Updated date", auto_now=True)

    def __str__(self):
        return self.created_date

    class Meta:
        abstract = True

class AbstractTextAreaOnly(AbstractTimeStampModel):
     """Abstract class for textfield"""

    about = models.TextField(
        verbose_name="Description",
        blank=True
    )

    def __str__(self):
        return "Textonly class-"+self.id

    class Meta:
        ordering = ['created_date']

models.py in core/models.py

(models.py在芯/ models.py)

from django.db import models
from .abstract_models import (
      AbstractTextAreaOnly,
)

Now I want to use this abstract class in about_app/ So my derived class in models.py inside about_app/models.py looks:

(现在,我想在about_app /中使用这个抽象类,因此我在about_app / models.pymodels.py中的派生类看起来是:)

  from django.db import models
  from gaasc_apps.core.models import(
      AbstractTextAreaOnly,
  )

  class GeneralInformation(AbstractTextAreaOnly):
     '''
     Gives general information of college
     '''

     class Meta():
         db_table = "general_information"
         verbose_name="General information"
         ordering=['created_date']

What I tried:

(我试过的)

I tried to registering using following ways:

(我尝试使用以下方式进行注册:)

method1:

(方法1:)

in admin.py in about_app/admin.py :

(在about.app/admin.py中的admin.py中 :)

from django.contrib import admin
from .models import (
    GeneralInformation,
)

@register(GeneralInformation)
class GeneralInformationAdmin(admin.ModelAdmin):
    pass

method 2:

(方法2:)

in about_app/admin.py

(在about_app / admin.py中)

from django.contrib import admin
from .models import (
    GeneralInformation,
)

class GeneralInformationAdmin(admin.ModelAdmin):
   pass

admin.site.register(GeneralInformation,GeneralInformationAdmin)

method3:

(方法3:)

in about_app/admin.py

(在about_app / admin.py中)

  ......
  class GeneralInformationAdmin(admin.ModelAdmin):
     readonly_fields = ('id','about','created_date','updated_date')
     list_display=('id','about','created_date')
     fieldsets=[
        ('Id',{'fields':['id']}),
        ('About',{'fields':['about']}),
        ('Created_Date',{'fields':['created_date']}),
     ]

     admin.site.register(GeneralInformation,GeneralInformationAdmin)

With all this solution GeneralInformation is not shown in admin panal/dashboard of django.

(有了所有这些解决方案,Django的管理面板/仪表板中未显示GeneralInformation。)

The solution seems to be simple but I don't know why its not working?

(解决方案似乎很简单,但我不知道为什么它不起作用?)

Some solution I tried are here:

(我尝试过的一些解决方案在这里:)

How to register inherited sub class in admin.py file in django?

(如何在Django的admin.py文件中注册继承的子类?)

Django admin model Inheritance is it possible?

(Django admin模型可以继承吗?)

Register abstract model in admin django 1.6

(在Django 1.6中注册抽象模型)

VERSION: Django-2.2 python-3.6

(版本:Django-2.2 python-3.6)

  ask by Bhoj translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...