开发者

Django Admin Interface: how to show different model based on a user's selection?

开发者 https://www.devze.com 2023-02-22 03:11 出处:网络
I am creating a Poll application using Django. The workflow for the Admin user is as follows: Admin clicks on the \"Add Question\" which shows the Admin a simple page with a dropdown list of 3 types

I am creating a Poll application using Django. The workflow for the Admin user is as follows:

  1. Admin clicks on the "Add Question" which shows the Admin a simple page with a dropdown list of 3 types(True/False,Multiple choice, Single choice) of Questions she can 开发者_运维技巧create. Basically there will be 3 types of Question Models for each of the kind.

  2. Based on the Admin's selection from the dropdown list, that particular model is shown and the admin can enter values for different fields of that model.

That's all! Seems pretty basic, but being a newbie in Django I can't find a solution. Any help will be greatly appreciated.

Thanks!


This isn't really behavior you can expect to be baked into django.

Take advantage of what django does ship with, the CRUD interfaces, and set up a start page that has a drop down list that redirects to the appropriate model via some JavaScript.

<script>
// I'm using jQuery since I can type it as fast as pseudo code...
$(function() {
    $("select").change(function() {
        window.location = $(this).val();
    })
})
</script>

<select>
    <option value="{% url admin:myapp_mymodel1_add %}">Model 1</option>
    <option value="{% url admin:myapp_mymodel2_add %}">Model 2</option>
    <option value="{% url admin:myapp_mymodel3_add %}">Model 3</option>
</select>


Do you have to use the admin for this? I think a series of common forms would serve you better if you want to create a custom workflow which involves processing the data of multiple forms.

You could tie your forms together using Django's FormWizard.

0

精彩评论

暂无评论...
验证码 换一张
取 消