Is there a way to force django to display nothing instead of "None" for a Decimal Field that's been left blank?
有没有办法强制django显示任何内容而不是“无”为一个空白的十进制字段?
In my template, I show a list of all the values for a particular field. Each value is hyperlinked to a page that displays the results of a query filtered on that value. But because there are some entries with null value, my list includes actual DecimalField entries and "None" for all those that are empty. When a user clicks on None, django throws a validation error because you cannot query a DecimalField using a string.
在我的模板中,我显示了特定字段的所有值的列表。每个值都超链接到一个页面,该页面显示在该值上过滤的查询结果。但是因为有一些空值的条目,我的列表包括实际的DecimalField条目和所有空的条目的“无”。当用户单击None时,django会抛出验证错误,因为您无法使用字符串查询DecimalField。
I could write if statements checking all instances of decimal fields for Nones and skipping them, but that is far from an elegant solution. Any advice?
我可以编写if语句检查Nones的所有十进制字段实例并跳过它们,但这远非一个优雅的解决方案。任何建议?
This is one part of the code, though there are other templates that derive the None value in slightly different manners:
这是代码的一部分,尽管还有其他模板以稍微不同的方式派生None值:
{% for item in choices %}
<a href={% url app_views.field_choice item %}>{{ item }}</a><br>
{% endfor %}
2 个解决方案
#1
15
If you don't want to filter the list of values, you could use the built-in default
or default_if_none
template filters to control what's displayed, but with the example given above, you'd end up with empty links.
如果您不想过滤值列表,可以使用内置的默认或default_if_none模板过滤器来控制显示的内容,但是使用上面给出的示例,您最终会得到空链接。
{{ item|default_if_none:"" }}
Given your need to hyperlink and query on each value and a view which is going to show an error if not given a number, I'd filter the list when you're passing it to the template context:
鉴于您需要对每个值进行超链接和查询以及如果没有给出数字将显示错误的视图,我会在您将列表传递给模板上下文时对其进行过滤:
{"choices": [choice in choices where choice is not None]}
#2
0
Ok, so lets say you get Django to return an empty string instead od None for empty values.
好的,所以假设你让Django返回一个空字符串而不是空值的od。
So now what happens with this code:
那么现在这个代码会发生什么:
{% for item in choices %}
<a href={% url app_views.field_choice item %}>{{ item }}</a><br>
{% endfor %}
You will either:
你会:
- Get a bunch of empty links (
<a href="/field_choice/"></a>
) - 获取一堆空链接( )
- Or an exception form the url reverse mechanism (because the positional argument is required).
- 或者url反向机制的异常(因为需要位置参数)。
The method you don't want to use is much (!) more elegant:
你不想使用的方法更多(!)更优雅:
{% for item in choices %}
{% if item %}
<a href={% url app_views.field_choice item %}>{{ item }}</a><br>
{% endif %}
{% endfor %}
Of course you are also free to build a list without empty items beforehand: in the view, or even using a custom manager.
当然,您也可以事先建立一个没有空项目的列表:在视图中,甚至使用自定义管理器。
#1
15
If you don't want to filter the list of values, you could use the built-in default
or default_if_none
template filters to control what's displayed, but with the example given above, you'd end up with empty links.
如果您不想过滤值列表,可以使用内置的默认或default_if_none模板过滤器来控制显示的内容,但是使用上面给出的示例,您最终会得到空链接。
{{ item|default_if_none:"" }}
Given your need to hyperlink and query on each value and a view which is going to show an error if not given a number, I'd filter the list when you're passing it to the template context:
鉴于您需要对每个值进行超链接和查询以及如果没有给出数字将显示错误的视图,我会在您将列表传递给模板上下文时对其进行过滤:
{"choices": [choice in choices where choice is not None]}
#2
0
Ok, so lets say you get Django to return an empty string instead od None for empty values.
好的,所以假设你让Django返回一个空字符串而不是空值的od。
So now what happens with this code:
那么现在这个代码会发生什么:
{% for item in choices %}
<a href={% url app_views.field_choice item %}>{{ item }}</a><br>
{% endfor %}
You will either:
你会:
- Get a bunch of empty links (
<a href="/field_choice/"></a>
) - 获取一堆空链接( )
- Or an exception form the url reverse mechanism (because the positional argument is required).
- 或者url反向机制的异常(因为需要位置参数)。
The method you don't want to use is much (!) more elegant:
你不想使用的方法更多(!)更优雅:
{% for item in choices %}
{% if item %}
<a href={% url app_views.field_choice item %}>{{ item }}</a><br>
{% endif %}
{% endfor %}
Of course you are also free to build a list without empty items beforehand: in the view, or even using a custom manager.
当然,您也可以事先建立一个没有空项目的列表:在视图中,甚至使用自定义管理器。