Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the value of choice in the model through get_FOO_display by django

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "django how to find the choice value in the model through get_FOO_display", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how django finds the value of choice in the model through get_FOO_display".

In the models.py of django, we define some tuples of choices, similar to some dictionary values. For example, an order form may have multiple states, then the order status field can be choice, and status stores numeric integers in the database.

Status = models.IntegerField (verbose_name= "order status", choices=order_status_choice,)

The status meaning corresponding to the order status number can be bound with tuples, which is more flexible to write, and the number can be changed at will.

Class MainOrderStatus (object): wait_order_check = 1 wait_finance_check = 2 wait_out_stock_confirm = 3 wait_delivery_confirm = 4 success = 5 invalid = 6 cancel = 7

Order_status_choice = (MainOrderStatus.wait_order_check, "pending order Review"), (MainOrderStatus.wait_finance_check, "pending Financial Review"), (MainOrderStatus.wait_out_stock_confirm, "pending delivery confirmation"), (MainOrderStatus.wait_delivery_confirm, "Shipping confirmation"), (MainOrderStatus.success, "completed"), (MainOrderStatus.invalid, "invalidated"), (MainOrderStatus.cancel "cancelled")

However, when the order status is displayed on the front page, you can use get_FOO_display () to get the order status corresponding to the number in the django framework. In this example, you can use order_status = main_order.get_status_display () to get the status of the order. FOO is a field in the corresponding model.

Try not to have a fixed hard code in the code, such as a certain judgment condition, and judge that the status of the order is pending order review, you might write:

If status = = 1: pass

The more flexible way to write it would be like this:

If status = = MainOrderStatus.wait_order_check: pass so far, I believe you have a deeper understanding of "how django looks up the choice value in the model through get_FOO_display". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report