Django 管理存储动态表单集添加了 ajax
我目前正在使用 django admin 实现一个解决方案,它允许用户在数据库中定义产品,然后自定义属性和详细信息,更多详细信息可以通过公共属性聚合,这允许我使用 ajax 查询自定义视图返回一些 JSON 数据,以在同一表单集视图中直接自动构建我需要的表单字段(操作 DOM)。
当前的数据库设计遵循以下模式: 目录(名称、描述、照片)
产品(rel_catalog、名称、基本价格、照片、制造商_电子邮件)
产品详细信息(rel_product、rel_attribute、percentage_price、fixed_price)
ProductAttribute(rel_product, name, description)
如您所见,我有一个目录,其中可以有更多产品,每个产品有很多详细信息,按属性聚合。然后,我简单地默认显示目录,然后选择该目录的所有可用产品,然后选择正确的产品,我获得完整的表单(每行都有一个带有 ProductAttribute.name 的标签和带有相关 ProductDetail 的选择)。
一切都很好,但是当有人填写表格(使用所选产品下订单)时,我还需要将这些引用存储在数据库中。该表单显示为 StackedInline(ModelAdmin 用于订单)。 我不知道每个产品可能有多少个选项,所以我想使用这个设计来跟踪订单: 订单(客户、状态、备注、tot_price、inserted_by)
OrderItem(rel_order, Catalog, Product, unit_price)
但我不知道如何存储动态添加的输入... 我想实现 OrderItemProperty(rel_orderitem, rel_productdetail, rel_productattribute)
来存储每个单个输入......但是如何循环遍历这个未知字段?
也许你建议更好的设计?
如果您需要更多代码,请提出要求,我将使用 Pastebin 链接进行回复。 谢谢。
I'm currently implementing a solution using django admin, it allows users to define in the db a product, and then custom attributes and details, more details may be aggregated by a common attribute, this allows me to query with ajax a custom view that returns some JSON data to build automagically the form fields that I need directly in the same formset view (manipulating the DOM).
The current DB design follows this schema:Catalog(name, description, photo)
Product(rel_catalog, name, base_price, photo, manufacturer_email)
ProductDetail(rel_product, rel_attribute, percentage_price, fixed_price)
ProductAttribute(rel_product, name, description)
As you may see I have a catalog, where there can be more products, a lot of details per product, aggregated by attributes. Then I simple show by default the Catalog, then the select with all available products for that catalog, then, choosing the right Product, I obtain the complete form (each row has a label with ProductAttribute.name and a select with related ProductDetail).
All works pretty dam good, but I also need to store this references in the DB when someone completes the form (making an order with choosen products). This forms are displayed as StackedInline (the ModelAdmin is for the Order).
I don't know how many options there may be per product so I was thinking to use this design to track orders:Order(customer, status, notes, tot_price, inserted_by)
OrderItem(rel_order, catalog, product, unit_price)
But I don't know how to store the dynamic added inputs...
I was thiking to implement OrderItemProperty(rel_orderitem, rel_productdetail, rel_productattribute)
to store each single input... but how do I loop over this unknown fields?
Maybe do you suggest a better design?
If you need more code just ask for it and I'll reply with a pastebin link.
Thankyou.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我得到了一个可行的解决方案,
我创建了一个自定义视图,覆盖默认的“add/”视图,这样我可以自定义我想要的任何内容,并且我可以读取处理每个验证的 POST 数据,然后将数据放入正确的模型。
Finally I got a working solution,
I've created a custom view, overriding the default "add/" view, this way I can customize whatever I want to and I can read the POST data handling each validation, putting then the data in the right model.