Django:根据选择选项更改内联 [英] Django: change inlines based on select option
问题描述
models.py :
class Category(models.Model):
title = models.CharField()
CHOICES =(
('1','1'),
('2','2'),
('3','3'),
)
type = models $ char $($)
class视频(models.Model):
category = models.ForeignKey(Category)
class Picture(models.Model) :
video = models.ForeignKey(视频)
title = models.Charfield()
admin.py :
class PictureInline(admin.TabularInline):
model = Picture
extra = 5
class VideoAdmin(admin.ModelAdmin):
inlines = [PictureInline,]
问题:
当我添加视频i并选择类别,我可以根据我为视频选择的类别类型进行多媒体显示PictureInline?
如果我在选择列表中选择第一个类别,我想要在管理员中看到PictureInline,如果我选择其他类别,我不想看到PictureInline。
可以吗?
PS :我发现 https: //github.com/digi604/django-smart-selects ,但没有找到这样的内联功能
只需使用JavaScript来动态隐藏/显示内联集合。内联集合的ID始终为#[related_name] -group
。
(function($){
$(document).ready(function(){
function togglePictureInline(selected){
$ .getJSON('/ ajax / category -type /',{id:selected},function(data,jqXHR){
if(data [0] .fields.type == 1)
$('#pictures-group')。 show();
else
$('#图片集合')hide();
});
}
var $ category = $('#id_category');
togglePictureInline($ category.val());
$ category.change(function(){
togglePictureInline($(this) );
});
});
})(django.jQuery);
yourapp / views.py
$来自django.shortcuts的b $ b
导入get_list_or_404
从django.core导入序列化程序
def ajax_category_type(request):
id = request.GET.get('id')
categories = get_list_or_404(Category,id = id)
data = serializers.serialize('json',categories,fields =('type'))
return HttpResponse(data,mimetype ='application / json')
将以下内容添加到 VideoAdmin
:
class VideoAdmin(admin.ModelAdmin):
class Media:
js =('path / to / this.js',)
或覆盖 templates / yourapp / video / change_form.html
与:
{%extends'admin / change_form.html'%}
{%block extrahead%}
{{block.super}}
< script src = path / to / this.jstype =text / javascript>< / scri PT>
{%endblock%}
更新:
我已将JavaScript改为包含AJAX请求。您将不得不使用AJAX,因为您必须先获取所选类别,然后才能获取其类型。我还添加了一个基本视图,您可以使用它来返回您需要的数据。你只需要查看你的urls.py并更改AJAX调用中的URL来匹配。
Category has "types" (for example, three types of categories). Each category may have any number of Videos. And each Video, published in Category of '1' type may have any number of Pictures. But for Video, published in '2' and '3' Category types there are no Pictures.
models.py:
class Category(models.Model):
title = models.CharField()
CHOICES = (
('1','1'),
('2','2'),
('3','3'),
)
type = models.CharField(choices=CHOICES)
class Video(models.Model):
category = models.ForeignKey(Category)
class Picture(models.Model):
video = models.ForeignKey(Video)
title = models.Charfield()
admin.py:
class PictureInline(admin.TabularInline):
model = Picture
extra = 5
class VideoAdmin(admin.ModelAdmin):
inlines = [PictureInline,]
question:
When i add Video item, and selecting Category for it, how can i dinamically show PictureInline based on what type of Category i selected for Video?
If i select first Category in select list, i want to be ablle to see PictureInline in admin, and if i select other Categories, i didn't want to see PictureInline.
Is it possible?
PS: I found https://github.com/digi604/django-smart-selects but didn't find such functionality for inlines
Just use JavaScript to dynamically hide/show the inline set. The id of the inline set is always #[related_name]-group
.
(function($){
$(document).ready(function(){
function togglePictureInline(selected) {
$.getJSON('/ajax/category-type/', { id: selected }, function (data, jqXHR) {
if (data[0].fields.type == 1)
$('#pictures-group').show();
else
$('#pictures-group').hide();
});
}
var $category = $('#id_category');
togglePictureInline($category.val());
$category.change(function(){
togglePictureInline($(this).val());
});
});
})(django.jQuery);
yourapp/views.py
from django.shortcuts import get_list_or_404
from django.core import serializers
def ajax_category_type(request):
id = request.GET.get('id')
categories = get_list_or_404(Category, id=id)
data = serializers.serialize('json', categories, fields=('type',))
return HttpResponse(data, mimetype='application/json')
Add the following to VideoAdmin
:
class VideoAdmin(admin.ModelAdmin):
...
class Media:
js = ('path/to/this.js',)
Or override templates/yourapp/video/change_form.html
with:
{% extends 'admin/change_form.html' %}
{% block extrahead %}
{{ block.super }}
<script src="path/to/this.js" type="text/javascript"></script>
{% endblock %}
UPDATE:
I've changed the JavaScript above to include an AJAX request. You're going to have to use AJAX because you've got to fetch the selected Category before you can get the type of it. I've also added a basic view you can use to return the data you need. You just need to hook the view up in your urls.py and change the URL in the AJAX call to match.
这篇关于Django:根据选择选项更改内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!