如何在管理员中使用contenttypes自定义模型的显示? [英] How can I customize the display of a model using contenttypes in the admin?

查看:156
本文介绍了如何在管理员中使用contenttypes自定义模型的显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些模型:

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

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

class ProjectB(ProjectA):
pass

class Attachment(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models .positiveIntegerField()
project = generic.GenericForeignKey(content_type,object_id)
file = models.FileField(upload_to =。)
/ pre>

我正在注册管理员的所有模型,我正在注销组,用户和站点。事情是,当我访问管理员中的附件时,我看到它呈现如下:





在内容类型选择中,我看到这个列表:





附件原因有一个GenericForeignKey是因为ProjectA和ProjectB需要访问它。我知道ProjectA和ProjectB是一样的,但是它们是存储在2个独立的表中的要求。如何使管理员可以使用Attachment类?我知道如何使用普通视图中的内容类型,但是从管理员不能使用。



在附件类中,我只想选择项目A或项目B,然后列出所有项目A或所有项目B,然后是我要附加的文件。



管理员可以这样做吗?我需要向用户显示Object Id列吗?

解决方案

如果我没有错,你想要这个。 http://code.google.com/p/django-genericadmin/



我的建议会有所不同。您将在ProjectA中添加一个更多的表单,ProjectB作为内联。在您的admin.py中

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

来自myproject.myapp.models import附件,ProjectA,ProjectB

class Attachmentline(generic.GenericTabularInline):#or generic.GenericStackedInline,这有不同的视觉布局。
model = Attachment

class ProjectAdmin(admin.ModelAdmin):
inlines = [
附件线,
]

管理员.site.register(ProjectA,ProjectAdmin)
admin.site.register(ProjectB,ProjectAdmin)

去您的ProjectA或ProjectB管理员,并看到新的管理员。



这不是你想要的,但它可以帮助你。否则您需要检查第一个链接。


I have these models:

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

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

class ProjectB(ProjectA):
  pass

class Attachment(models.Model):
  content_type    = models.ForeignKey(ContentType)
  object_id       = models.PositiveIntegerField()
  project         = generic.GenericForeignKey("content_type","object_id")
  file            = models.FileField(upload_to=".")

I'm registering all the models for the admin, and I'm unregistering Group, User and Site. The thing is, when I access the Attachment in the admin, I see it rendered like this:

In the Content type select, I see this list:

The reason Attachment has a GenericForeignKey is because both ProjectA and ProjectB need to access it. I know that ProjectA and ProjectB are identical, but it's a requirement that they are stored in 2 separate tables. How could I made the Attachment class useable from the admin? I know how to use contenttypes from normal views, but from the admin not.

In the Attachment class I would only like to have a select for Project A or Project B, and then a list of all Project A's or all Project B's, followed by the file that I want to attach.

Is such a thing possible from the Admin? Will I need to show the user the Object Id column?

解决方案

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.

这篇关于如何在管理员中使用contenttypes自定义模型的显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆