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:
我正在为管理员注册所有模型,我正在取消注册Group,User和Site。问题是,当我访问管理员中的附件时,我看到它呈现如下:
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.
Attachment具有GenericForeignKey的原因是因为ProjectA和ProjectB都需要访问它。我知道ProjectA和ProjectB是相同的,但要求它们存储在2个单独的表中。我怎样才能使Adminment中的Attachment类可用?我知道如何使用普通视图中的contenttypes,但不是来自admin。
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.
在Attachment类中,我只想选择项目A或项目B,然后选择所有项目A或所有项目B的列表,然后是我想要附加的文件。
Is such a thing possible from the Admin? Will I need to show the user the Object Id column?
管理员可以这样做吗?我是否需要向用户显示Object Id列?
2 个解决方案
#1
8
if I'm not wrong, you want this. http://code.google.com/p/django-genericadmin/
如果我没错,你想要这个。 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
我的建议会有所不同。您将在ProjectA中添加一个更多的表单,ProjectB作为内联。在你的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.
去你的ProjectA或ProjectB管理员,看看新的管理员。
this isn't what you want but it can help you. otherwise you need check first link.
这不是你想要的,但它可以帮助你。否则你需要检查第一个链接。
#2
1
You should notice that
你应该注意到这一点
" know that ProjectA and ProjectB are identical, but it's a requirement that they are stored in 2 separate tables"
“知道ProjectA和ProjectB是相同的,但要求它们存储在2个单独的表中”
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:
是不是真的正确。所有数据都存储在app_projecta表中,并且(仅)某些指针保存在表app_projectb中。如果你已经走在这条道路上,我建议从这开始:
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...
这已经让你更接近你想要的地方......
#1
8
if I'm not wrong, you want this. http://code.google.com/p/django-genericadmin/
如果我没错,你想要这个。 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
我的建议会有所不同。您将在ProjectA中添加一个更多的表单,ProjectB作为内联。在你的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.
去你的ProjectA或ProjectB管理员,看看新的管理员。
this isn't what you want but it can help you. otherwise you need check first link.
这不是你想要的,但它可以帮助你。否则你需要检查第一个链接。
#2
1
You should notice that
你应该注意到这一点
" know that ProjectA and ProjectB are identical, but it's a requirement that they are stored in 2 separate tables"
“知道ProjectA和ProjectB是相同的,但要求它们存储在2个单独的表中”
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:
是不是真的正确。所有数据都存储在app_projecta表中,并且(仅)某些指针保存在表app_projectb中。如果你已经走在这条道路上,我建议从这开始:
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...
这已经让你更接近你想要的地方......