[英]How do I change the value of true or false to a text value type it I am checking the row in the entire table Note the table is of type datatables?
使用 django 如何將 true 或 false 的值更改為文本值類型我正在檢查整個表中的行
名稱項目 | 類型項目 | 積極的 |
---|---|---|
項目 1 | 15.0 | 真的 |
項目2 | 15.0 | 錯誤的 |
更改值行活動使用 jquery 或 django 文件 view.py:
名稱項目 | 類型項目 | 積極的 |
---|---|---|
項目 1 | 15.0 | 顯示 |
項目2 | 15.0 | 采場 |
請幫忙
最優雅的方法可能是為BooleanField
指定選擇,因此:
from django.db import models
ACTIVE_CHOICES = [
(False, 'stope')
, (True, 'displayed')
]
class MyModel(models.Model):
# …
active = models.BooleanField(choices=ACTIVE_CHOICES)
然后你可以在模板中渲染相應的標簽:
{{ mymodelobject.get_active_display }}
默認情況下,它還將在ModelForm
、 ModelAdmin
和序列化程序中使用這些選項。
沒有代碼,很難給出准確的答案。
但是在javascript中基於布爾值設置值的最簡單方法是使用三元運算符。
如果為 true,active 將設置為“displayed”,如果為 false,active 將設置為“stope”
活動 = 布爾值? “顯示”:“采場”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.