简体   繁体   English

如何在Django中创建带有链接以更改模型实例的布尔值的表?

[英]How to create a table with links to change boolean values of a model instance in Django?

I have a ListView where I show customer orders in multiple tables. 我有一个ListView,可以在多个表中显示客户订单。 There is one table for orders that are sent (not yet marked as 'received'), one table for 'received' orders, one for 'processed' order and one for 'delivered' orders. 有一张表用于发送的订单(尚未标记为“已接收”),一张表用于“已接收”订单,一张用于“已处理”订单,另一张用于“已交付”订单。

This list is a staff list, only certain users have access to it. 此列表是人员列表,只有某些用户可以访问它。

What I would like to do is to add a button (link or maybe a submit button for a form) to each order row. 我想做的是向每个订单行添加一个按钮(链接或表单的提交按钮)。

For example, in the table for the sent orders (not yet 'received') there should be a button that says 'Mark as received' ('Kvittera' in Swedish), in the 'received' table, the button should say 'Mark as processed' ('Klar' in Swedish) and so forth. 例如,在已发送订单(尚未“接收”)表中,应该有一个标有“标记为已接收”的按钮(瑞典语中为“ Kvittera”),在“已接收”表中,该按钮应显示为“标记”加工”(瑞典语中的“ Klar”)等等。

This is a picture of how I would like it to look (sorry for the Swedish): 这是我想要的样子的图片(对不起瑞典语):

图片外观

The model has three boolean fields; 该模型具有三个布尔字段; order_received , order_processed and order_delivered . order_receivedorder_processedorder_delivered If I were to push the button 'Kvittera' in the table, the boolean value of order_received should change from False to True , and so forth. 如果我在表中按下按钮“Kvittera”的布尔值order_received应该从改变FalseTrue ,等等。 Note that you should never be able to change it back to False . 请注意,您永远不能将其更改回False

I can set up a button/link that GETs a URL, ie http://example.com/orders/1/receive which runs a view that does exactly this and then redirects to the list page again. 我可以设置一个获取URL的按钮/链接,即http://example.com/orders/1/receive ,该按钮/链接会运行一个完全执行此操作的视图,然后再次重定向到列表页面。 I have several problems with this though: 我对此有几个问题:

  1. You should not change anything with GET (use POST, PUT or PATCH) 您不应使用GET更改任何内容(使用POST,PUT或PATCH)
  2. You should not use verbs in URLs (receive in this case) 您不应该在网址中使用动词(在这种情况下会收到)

One solution to this problem, I think, is to create a form which POSTs data instead. 我认为,解决此问题的一种方法是创建一种表单来发布数据。 But in this form there should be no fields, and one form is needed per order row. 但是以这种形式应该没有字段,并且每个订单行都需要一种形式。 The form is essentially only a submit button. 该表单实际上只是一个提交按钮。

As well, every form should be bound to the instance of the order in question and should programmatically only change one value from False to True. 同样,每种形式都应绑定到所讨论订单的实例,并且应以编程方式仅将一个值从False更改为True。 How can I accomplish this? 我该怎么做? Am I overthinking it or completely off track? 我是想得太多还是完全偏离了轨道?

I think you are on the right track with your solution to post data through forms, sending data like : {'state': 'received'}. 我认为您的解决方案走上正确的轨道,可以通过表单发布数据,并发送数据,例如:{'state':'received'}。 Also, I guess that Django Formsets might be useful for your design: https://docs.djangoproject.com/en/1.9/topics/forms/formsets/ . 另外,我猜想Django Formsets可能对您的设计有用: https ://docs.djangoproject.com/en/1.9/topics/forms/formsets/。 Change the doc version if you don't use Django 1.9 如果您不使用Django 1.9,请更改文档版本

Another solution would be to have a dropdown/ ChoiceField with id-s of all the objects for which it is possible to change order_received to True. 另一个解决方案是使所有对象的id-s都具有dropdown / ChoiceField ,可以将order_received更改为True。 You can make two more dropdowns for order_processed and order_delivered . 您可以为order_processedorder_delivered创建两个下拉菜单。 Then, hide whole the form, and with some JS fiddling make the buttons to update the form values and send the form. 然后,隐藏整个表单,并用一些JS修饰使按钮更新表单值并发送表单。

But, being honest, it will just not reuse the built in Django functions, so I would still advice to say what @phiberjenz mentions and make a formset . 但是,老实说,它不会重复使用内置的Django函数,因此我仍然建议说出@phiberjenz提到的内容并制作一个formset If you don't want to use JS you could make the buttons next to the orders to work as submit buttons AND send the post that would be processed by your server-side form. 如果您不想使用JS,则可以将订单旁边的按钮用作提交按钮,然后发送将由服务器端表单处理的帖子。

I ended up creating a RedirectView that called a method on the instance changing the state of the order. 我最终创建了一个RedirectView,该实例在实例上调用了一个方法来更改订单状态。 This is called with a GET request, but super simple and according to https://docs.djangoproject.com/en/1.10/ref/class-based-views/base/ (at the bottom) the example shows exactly this. 这是通过GET请求调用的,但是非常简单,根据https://docs.djangoproject.com/en/1.10/ref/class-based-views/base/ (在底部),示例完全显示了这一点。

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

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