大多数djangonic解决方案类别 - 子类别 - 产品选择表单? [英] Most djangonic solution for Category - Subcategory - Product select form?

查看:98
本文介绍了大多数djangonic解决方案类别 - 子类别 - 产品选择表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们假设我们有这三个模型:

   -  cagegory(id,name)
- 子类别,id_category,name)
- 产品(id,id_sub_category,name)

每个类别有30个子类别,每个子类别有50个产品。



总产品= 30 x 30 x 50 = 45000



然后,在django项目的几个意见中,我想包括产品选择。



实现产品选择最多的djangonic解决方案是什么? / strong>

解决方案

Django中没有任何内容会为您解决这个用例,但是您可以使用组合在Django的事情做这项工作。



我假设你想要你的产品选择如下:




  • 用户选择类别。

  • i> Ajax请求将类别ID发送到视图;视图返回一个html
    字符串,由另一个视图渲染,其中包含子类别的
    a下拉列表,由类别过滤。
  • 与之前的过程相同发生,只有子类别ID被传递给视图,该视图返回产品的选择,由子类别过滤。



  • 您将需要使用视图,表单和一些JavaScript的组合来使其发挥作用。有一些很好的jQuery插件可以通过Ajax提交表单。视图可以返回呈现为字符串的HTML,您可以将其弹出到DOM中,并且您应该只需要一个附加到将父表单提交给将处理POST的URL的选择器的事件侦听器。



    希望能让你走上正轨。


    Let's suppose we have these three models:

     - cagegory    ( id, name ) 
     - subcategory ( id, id_category, name ) 
     - product     ( id, id_sub_category, name )
    

    with 30 categories, each category has 30 subcategories and each subcategory has 50 products.

    Total products = 30 x 30 x 50 = 45000

    Then, in severals views of django project I want to include a product selection.

    What is the most djangonic solution to implement the product selection?

    解决方案

    There's nothing built into Django that's going to solve this use case for you, however, you can use a combination of things in Django to make this work.

    I'm assuming you want your product selection to go like this:

    • Form has a select drop-down for category.
    • User selects category.
    • Ajax request sends category id to a view; view returns an html string resulting from the rendering of another view that contains a drop-down for the sub category, filtered by the category.
    • Same process as previous happens, only the sub category id is passed to a view that returns a select for the product, filtered by the sub-category.

    You're going to need to use a combination of views, forms and some JavaScript to make this work. There are some nice plugins for jQuery to submit forms via Ajax. Views can return HTML rendered as a string, which you can pop into the DOM, and you should only need one event listener attached to the select(s) that submits their parent form to the URL that will process the POST.

    Hope that gets you on the right track.

    这篇关于大多数djangonic解决方案类别 - 子类别 - 产品选择表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆