Django Admin:根据以前的字段值填充字段

编程入门 行业动态 更新时间:2024-10-22 21:30:35
本文介绍了Django Admin:根据以前的字段值填充字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在django管理员中有一个模型如下

I have a model in django admin as follows

ChoiceA= ( ("on-false","on-false"), ("on-true","on-true"), ) ChoiceB = ( ("always","always"), ("never","never"), ) id = models.CharField(verbose_name="Field",max_length=32) type = models.CharField(verbose_name="Expression",max_length=32) action = models.CharField(max_length=32, choices=x)

现在基于用户输入的类型,即如果用户输入type =a,则动作的选择应该设置为ChoiceA,如果用户输入type =b,则动作的选择应该被设置为ChoiceB。如何在Django Admin中实现?

Now based on the type entered by the user ie if user enters type = "a" then action's choices should be set to ChoiceA and if user enters type ="b" then action's choices should be set to ChoiceB. How can I achieve this in Django Admin?

编辑:

action_change.js

action_change.js

jQuery(document).ready(function(){ $("#id_type").change( function(event) { $.ajax({ "type" : "POST", "url" : "/action_choices/", "dataType" : "json", "cache" : false, "error" : alert("hello"), "success" : function(json) { $('#id_action >option').remove(); for(var j = 0; j < json.length; j++){ $('#id_action').append($('<option></option>').val(json[j][0]).html(json[j][1])); } } }); }); });

推荐答案

您可以使用Ajax和jQuery实现: p> models.py:

You can achieve it using Ajax and jQuery:

type = models.CharField(verbose_name="Expression",max_length=32) action = models.CharField(max_length=32, choices = (('', ''), ))

admin.py:

admin.py:

class MyModelAdmin(admin.ModelAdmin): list_display = ('type', ) class Media: js = ['/static/js/action_change.js'] admin.site.register(MyModel, MyModelAdmin)

urls.py:

urls.py:

url(r'^action_choices/', 'myproject.myapp.views.action_choices'),

views.py:

views.py:

def action_choices(request): action_list = [] ChoiceA = ("on-false", "on-true") ChoiceB = ("always", "never") action_type = request.GET.get('action_type') if str(action_type).lower() == 'a': choices = ChoiceA elif str(action_type).lower() == 'b': choices = ChoiceB else: choices = () [action_list.append((each,each)) for each in choices] json = simplejson.dumps(action_list) return HttpResponse(json, mimetype='application/javascript')

在静态文件夹中创建具有以下内容的文件 action_change.js ,并在 class Media 中定义正确的路径 ModelAdmin 。

Create the file action_change.js with following content in your static folder and define correct path in class Media of ModelAdmin.

(function($){ $(function(){ $(document).ready(function() { $('#id_type').bind('keyup', type_change); $('#id_action >option').show(); }); }); })(django.jQuery); // based on the type, action will be loaded var $ = django.jQuery.noConflict(); function type_change() { var action_type = $('#id_type').val(); $.ajax({ "type" : "GET", "url" : "/action_choices/?action_type="+action_type, "dataType" : "json", "cache" : false, "success" : function(json) { $('#id_action >option').remove(); for(var j = 0; j < json.length; j++){ $('#id_action').append($('<option></option>').val(json[j][0]).html(json[j][1])); } } })(jQuery); }

对于您提出的情况,这应该是正常的。我在下面给出我的建议:

This should work fine for the scenario you asked. And I'm giving my suggestion below:

type = models.CharField(verbose_name="Expression",max_length=32, choices = (('a', 'a'), ('b', 'b'), )) action = models.CharField(max_length=32, choices = (('', ''), ))

action_change。 js(第5行)

action_change.js (line 5)

$('#id_type').bind('change', type_change);

更多推荐

Django Admin:根据以前的字段值填充字段

本文发布于:2023-10-28 19:22:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537555.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   Django   Admin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!