How can I customize the display of a model using contenttypes in the admin?

时光怂恿深爱的人放手 提交于 2019-12-05 06:54:24

if I'm not wrong, you want this. http://code.google.com/p/django-genericadmin/

my advice will work differently. you will add a little more form in ProjectA, ProjectB as inline. in your admin.py

from django.contrib import admin
from django.contrib.contenttypes import generic

from myproject.myapp.models import Attachment, ProjectA, ProjectB

class Attachmentline(generic.GenericTabularInline): #or generic.GenericStackedInline, this has different visual layout.
    model = Attachment

class ProjectAdmin(admin.ModelAdmin):
    inlines = [
        Attachmentline,
    ]

admin.site.register(ProjectA, ProjectAdmin)
admin.site.register(ProjectB, ProjectAdmin)

go your ProjectA or ProjectB admin and see new admin.

this isn't what you want but it can help you. otherwise you need check first link.

Udi

You should notice that

" know that ProjectA and ProjectB are identical, but it's a requirement that they are stored in 2 separate tables"

is not really correct. All the data is stored in your app_projecta table, and (only) some pointers are kept in table app_projectb. If you are already going in this path, I would suggest starting with this instead:

class App(models.Model):
  name = models.CharField(max_length=100)

class Project(models.Model):
    name = models.CharField(max_length=100)
    app = models.ForeignKey(App)

class ProjectA(Project):
  pass

class ProjectB(Project):
  pass

class Attachment(models.Model):
  project = models.ForeignKey(Project)
  file = models.FileField(upload_to=".")  

This already gets you a bit closer to where you want to get...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!