Django 中的 GenericForeignKey 和 Admin [英] GenericForeignKey and Admin in Django
问题描述
假设我有一个 Post 对象,它可以包含图像、视频和其他媒体类型.我可以使用 GenericForeignKey
将它们链接在一起.类似的东西:
Let's say I have a Post object that can contain Images, Videos, and other media types. I can use a GenericForeignKey
to link them together. Something like:
class Post(models.Model):
title = models.CharField(...)
text = models.TextField(...)
class AudioMedia(models.Model):
...
class VideoMedia(models.Model):
...
class ImageMedia(models.Model):
...
class MediaObject(models.Model):
post = models.ForeignKey(Post)
order = models.IntegerField()
content_type_media = models.ForeignKey(
ContentType, limit_choices_to={
'model__in': (
'audiomedia',
'imagemedia',
'videomedia')
})
object_id_media = models.PositiveIntegerField()
obj = generic.GenericForeignKey('content_type_media', 'object_id_media')
现在我可以轻松创建管理界面,例如:
Now I can easily create an admin interface, like:
class MediaObjectAdminInLine(admin.StackedInline):
model = MediaObject
ct_field = "content_type_media"
ct_fk_field = "object_id_media"
extra = 0
class PostAdmin(admin.ModelAdmin):
inlines = [MediaObjectAdminInLine]
现在的问题:) 在 admin/中,我可以轻松地创建一个新帖子.对于帖子,我可以轻松添加更多 MediaObject.在面板中,我有一个下拉菜单可以选择类型(音频、视频等),但我必须手动输入我想与 Post 链接的对象的 ID.
Now the question :) In admin/, I can easily create a new Post. To the post, I can easily add more MediaObject. In the panel, I have a drop down menu to chose the type (audio, video, ...), but I have to manually enter the ID of the object I want to link with Post.
我尝试了各种扩展,包括 grappelli.有些提供了查找要链接到这里的对象的 ID 的能力.我希望能够在此处添加对象,例如,添加一个 AudioMedia、一个 VideoMedia、一个 ImageMedia,具体取决于我从下拉列表中选择的内容.
I have tried various extensions, including grappelli. Some provide the ability to lookup the ID of objects to link here. I want the ability to add objects here, eg, add an AudioMedia, a VideoMedia, an ImageMedia, depending on what I pick from the dropdown.
有什么建议吗?
推荐答案
您需要做很多工作才能完成这项工作.
You'd need to quite a bit of work to get this going.
- 您要求管理员根据您从下拉列表中选择的模型类型动态显示模型表单.
- Django 的管理员不这样做(也不做任何已知的扩展).
要完成这项工作,您必须:
To make this work, you'll have to:
- 编写一个自定义 JavaScript 事件处理程序,用于捕获模型选择下拉列表的 onchange.
- 然后调用 Django 的管理员并请求该模型的内联模型表单.
- 使用该模型表单更新当前的 HTML 页面.
- 然后您需要拦截父模型的modelform 的save() 方法以找出它正在处理的子模型表单,并将其正确保存到数据库中.
- 然后你需要弄清楚如何让父模型的modelform根据子模型正确显示合适的子模型的modelform.
听起来令人生畏?是.
这里有一个更简单的方法:
Here's an easier way:
只需一个媒体"模型.您将在模型中拥有一些仅对您的一种类型有效的字段(尽管存在大量交叉).
Just have a single "Media" model. You'll have a few fields on the model that are only valid for one of your types (though there's plenty of crossover).
使用该媒体类型的前缀命名特定于单个媒体类型的任何字段,即image_size'或
video_title`.
Name any fields that are specific to a single Media type with a prefix for that mediatype, i.e. image_size', or
video_title`.
将 JavaScript 处理程序附加到您的 ModelAdmin,它根据媒体类型的下拉列表有选择地显示和隐藏字段.像这样:
Attach a JavaScript handler to your ModelAdmin which selectively shows and hides fields based on a dropdown for the media type. Something like this:
class MediaAdmin(admin.ModelAdmin):
class Meta:
js = ["js/media-types.js",]
// media-type.js
(function($) {
$(document).ready(function(){
$('.module[id^=module] .row').hide();
$('.module[id^=module] .row.module').show();
$('.module[id^=module] .row.module select').each(function(){
if ($(this).val() != '')
{
var group = $(this).parent().parent().parent().parent();
var field = $(this).parent().parent().parent();
var mtype = $(this).val().toLowerCase();
if (mtype != '')
{
$('.row', group).not(field).slideUp('fast');
$('.row[class*="'+mtype+'"]', group).slideDown('fast');
$('.row[class*="all"]', group).slideDown('fast');
}
else
{
$('.row', group).not(field).slideUp('fast');
}
}
});
$('.module[id^=module] .row.module select').change(function(){
var group = $(this).parent().parent().parent().parent();
var field = $(this).parent().parent().parent();
var mtype = $(this).val().toLowerCase();
if (mtype != '')
{
$('.row', group).not(field).slideUp('fast');
$('.row[class*="'+mtype+'"]', group).slideDown('fast');
$('.row[class*="all"]', group).slideDown('fast');
}
else
{
$('.row', group).not(field).slideUp('fast');
}
});
});
})(django.jQuery);
这篇关于Django 中的 GenericForeignKey 和 Admin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!