简体   繁体   English

根据Django Admin中的另一个保管箱选项预填充保管箱

[英]Prepopulate drop-box according to another drop-box choice in Django Admin

I have models like this: 我有这样的模型:

class User(models.Model): 
    Switch = models.ForeignKey(Switch, related_name='SwitchUsers') 
    Port = models.ForeignKey(Port) 
class Switch(models.Model): 
    Name = models.CharField(max_length=50) 
class Port(models.Model): 
    PortNum = models.PositiveIntegerField() 
    Switch = models.ForeignKey(Switch, related_name = "Ports") 

When I'm in Admin interface and choose Switch from Switches available, I would like to have Port prepopulated accordingly with Ports from the related Switch. 当我进入管理界面并选择“可用的交换机中的交换机”时,我希望相应地使用相关交换机中的端口预先填充端口。 As far as I understand I need to create some JS script to prepopulate it. 据我了解,我需要创建一些JS脚本来进行预填充。 Unfortunately I don't have this experience, and I would like to keep things simple as it possible and don't rewrite all Django admin interface. 不幸的是,我没有这种经验,我想使事情尽可能简单,并且不要重写所有的Django管理界面。 Just add this functionality for one Field. 只需为一个字段添加此功能。

Could you please help me with my problem? 你能帮我解决我的问题吗? Thank you. 谢谢。

You are correct that you will need some js to create this. 您是正确的,您将需要一些js来创建它。 You don't need to re-write the django admin interface. 您无需重新编写django管理界面。 You only need to customize it. 您只需要自定义它。 This type of a thing requires a few things. 这种类型的东西需要一些东西。

Start here: http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#modeladmin-objects 从这里开始: http : //docs.djangoproject.com/en/1.2/ref/contrib/admin/#modeladmin-objects

To include your javascript use the Media class . 要包含您的JavaScript,请使用Media类

The django forms give all form inputs consistent ids so it is easy to manipulate them. django表单为所有表单输入提供一致的ID,因此易于操作。 You will need, however, to "pass" your relationship to the client. 但是,您将需要将您的关系“传递”给客户。 I mean, on the client you'll need to know which Ports go with which Switch . 我的意思是,在客户端上,您需要知道哪个Ports与哪个Switch一起使用。 Depending on how much data you have, there are a couple of approaches for this: 根据您拥有的数据量,可以采用两种方法:

  • Encode the relationship in json and output it in a tag by customizing the admin template for your model. 通过为模型自定义管理模板 ,将关系编码为json并将其输出到标签中。

  • Use ajax. 使用ajax。 You'll need at least one extra view that takes a Switch and returns a json list (or something similar) for the Ports that go with it. 您至少需要一个额外的视图,该视图需要一个Switch并为与之配套的Ports返回一个json列表(或类似内容)。

The javascript maniuplation should be straight forward: you want to bind the onChange() event of the dropbox to a function that strips all but the relevant Ports in the Port dropbox. javascript操作应该很简单:您要将Dropbox的onChange()事件绑定到一个功能,该功能会剥离Port下拉框中的所有(但不包括相关Ports

I'd suggest doing your DOM manipulation using jquery . 我建议使用jquery进行DOM操作。

You can end up with a simpler solution than provided above. 您可以得到比上面提供的解决方案更简单的解决方案。 Instead of customizing template and putting json in a tag, put this json in the beginning of you js file, also put there $().ready call that will assign the listener to the select-box change event, and reference this js file into the ModelAdmin's Media. 无需自定义模板并将json放在标签中,而是将此json放在js文件的开头,还放在$()。ready调用中,它将侦听器分配给select-box change事件,然后将此js文件引用到ModelAdmin的媒体。

class MyAdmin(admin.ModelAdmin):
    class Media:
        js = ("my_code.js",)

Tip: use Firebug or Chrome inspector to investigate the real names of HTML fields on the form. 提示:使用Firebug或Chrome检查器调查表单上HTML字段的真实名称。

I understand that this is a shortcut, and may not be optimal, but have you considered combining the two select widgets into a single one using optgroups? 我知道这是一个捷径,可能不是最佳选择,但是您是否考虑过使用optgroups将两个选择的小部件合并为一个? It would save you from writing custom javascript. 这样可以避免编写自定义JavaScript。

Since it's a ForeignKey, if they select a port, you know what the switch is. 由于这是外键,因此如果他们选择端口,您就会知道交换机是什么。 You could have the switches be optgroups, with a value of "", so they can't save after selecting just a switch. 您可以将开关设为optgroup,其值为“”,因此仅选择一个开关后就无法保存。 Here are the steps I can see: 这是我可以看到的步骤:

  1. Customize the form fields to omit switch . 自定义表单字段以忽略switch
  2. Change the title of the port form field to Switch/Port . port表单字段的标题更改为Switch / Port
  3. Change the choices of the port form field to a tuple, generated from the switches/ports. port形式字段的choices更改为从交换机/端口生成的元组。
  4. Add a custom view for saving that gets the switch from the port and sets the switch of the user to it. 添加用于保存的自定义视图,该视图从端口获取交换机并将用户的交换机设置为该端口。

If the users/stakeholders would find this acceptable, it's a way to get around JavaScript programming that involves a fair bit of Django programming. 如果用户/利益相关者认为这是可以接受的,那么这是一种避免JavaScript编程的方法,其中涉及相当一部分Django编程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM