• <bdo id='lS1sN'></bdo><ul id='lS1sN'></ul>
  • <i id='lS1sN'><tr id='lS1sN'><dt id='lS1sN'><q id='lS1sN'><span id='lS1sN'><b id='lS1sN'><form id='lS1sN'><ins id='lS1sN'></ins><ul id='lS1sN'></ul><sub id='lS1sN'></sub></form><legend id='lS1sN'></legend><bdo id='lS1sN'><pre id='lS1sN'><center id='lS1sN'></center></pre></bdo></b><th id='lS1sN'></th></span></q></dt></tr></i><div id='lS1sN'><tfoot id='lS1sN'></tfoot><dl id='lS1sN'><fieldset id='lS1sN'></fieldset></dl></div>
    <tfoot id='lS1sN'></tfoot>

    <small id='lS1sN'></small><noframes id='lS1sN'>

        <legend id='lS1sN'><style id='lS1sN'><dir id='lS1sN'><q id='lS1sN'></q></dir></style></legend>
      1. Django 管理模型继承是否可能?

        Django admin model Inheritance is it possible?(Django 管理模型继承是否可能?)
        <tfoot id='R3Fn8'></tfoot><legend id='R3Fn8'><style id='R3Fn8'><dir id='R3Fn8'><q id='R3Fn8'></q></dir></style></legend>

          <small id='R3Fn8'></small><noframes id='R3Fn8'>

              <tbody id='R3Fn8'></tbody>
              <bdo id='R3Fn8'></bdo><ul id='R3Fn8'></ul>
                <i id='R3Fn8'><tr id='R3Fn8'><dt id='R3Fn8'><q id='R3Fn8'><span id='R3Fn8'><b id='R3Fn8'><form id='R3Fn8'><ins id='R3Fn8'></ins><ul id='R3Fn8'></ul><sub id='R3Fn8'></sub></form><legend id='R3Fn8'></legend><bdo id='R3Fn8'><pre id='R3Fn8'><center id='R3Fn8'></center></pre></bdo></b><th id='R3Fn8'></th></span></q></dt></tr></i><div id='R3Fn8'><tfoot id='R3Fn8'></tfoot><dl id='R3Fn8'><fieldset id='R3Fn8'></fieldset></dl></div>
                • 本文介绍了Django 管理模型继承是否可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  管理模型中是否可以继承?

                  Is inheritance possible in admin Models ?

                  例如考虑以下,

                  文件: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()
                  

                  ModelAdmin

                  文件: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 )
                  

                  编辑:

                  我根据建议修改了上面的模型,

                  I've modified the above model based on suggestions ,

                  如果 admin.py 是这样的,我没有收到任何错误,并且模型出现在 admin 上.

                  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 )
                  

                  但是如果我这样修改它

                  class AbstractAdminModel(  admin.ModelAdmin  ):
                      fields = ['author' , 'editor' , 'created_at' , 'edited_at']
                  
                  
                  class Admin_Topic( AbstractAdminModel ):
                      pass
                  
                  admin.site.register( Topic , Admin_Topic )
                  

                  我收到以下错误:

                  这是一个堆栈跟踪 链接

                  问题:该模型甚至没有出现在管理页面上

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

                  额外信息:

                  使用 django 1.2.5 和 pinax 0.7.2、Ubuntu 11.04、python 2.7.1+

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

                  推荐答案

                  也许你的答案有点晚了,但我认为其他人可能会遇到类似的问题 - 就像我一样.

                  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])
                  

                  这篇关于Django 管理模型继承是否可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                  <tfoot id='iBsHW'></tfoot>

                    <legend id='iBsHW'><style id='iBsHW'><dir id='iBsHW'><q id='iBsHW'></q></dir></style></legend>
                    1. <small id='iBsHW'></small><noframes id='iBsHW'>

                        <tbody id='iBsHW'></tbody>

                      • <i id='iBsHW'><tr id='iBsHW'><dt id='iBsHW'><q id='iBsHW'><span id='iBsHW'><b id='iBsHW'><form id='iBsHW'><ins id='iBsHW'></ins><ul id='iBsHW'></ul><sub id='iBsHW'></sub></form><legend id='iBsHW'></legend><bdo id='iBsHW'><pre id='iBsHW'><center id='iBsHW'></center></pre></bdo></b><th id='iBsHW'></th></span></q></dt></tr></i><div id='iBsHW'><tfoot id='iBsHW'></tfoot><dl id='iBsHW'><fieldset id='iBsHW'></fieldset></dl></div>

                          • <bdo id='iBsHW'></bdo><ul id='iBsHW'></ul>