English 中文(简体)
Django:从基于其价值的模式中恢复田地的选择?
原标题:Django: returning a selection of fields from a model based on their values?
  • 时间:2010-05-20 15:58:13
  •  标签:
  • django

我正在研究一些我无法控制的数据。 我只想回来谈谈我模式中某些不感兴趣的价值观的领域(如0、X或-),并在模板中单独加以利用。

我的模式就是这样:

class Manors(models.Model):
    structidx = models.IntegerField(primary_key=True, verbose_name="ID")    
    hills = models.CharField(max_length=100, null=True, blank=True, verbose_name="Number of fields") 

在我的模板中,我回去了一位马诺尔的书记,而我也希望像今天这样的产出。 山区没有兴趣:

{% for manor in manors %}
{% if manor.hills %}<li>Hills blah blah: {{ manor.hills }}</li>{% endif %}
{% endfor %}

我想避免模板过于逻辑。 理想的情况是,单单单单单单单单单单不会回到所附的不感兴趣的领域,那么,我只能读到<条码>>%,如果是脑./条码>。

页: 1 我可以像以下那样使用一位词典,但认为其效率非常低,最好只把一个有意思的田地回来。 这是可能的吗? 页: 1

我尝试拟订一种模式方法,将有趣价值观的字典归来,例如:

def get_field_dictionary(self):
    interesting_fields = {}
    for field in Manors._meta.fields:
        if field.value_to_string(self) != "N" and field.value_to_string(self) != "0" and field.value_to_string(self) != "-" and field.value_to_string(self) != "X":
            interesting_fields[field.name] = field.value_to_string(self)
    return interesting_fields

但我不知道如何在模板中获取字典的个人价值:

{% if manor.get_field_dictionary[ hills ] %}<li>Hills blah blah: {{ manor.get_field_dictionary[ hills ] }}</li>{% endif %}

a 给出一个模板。 是否有更好的办法这样做?

<>UPDATE:这是最终的,作为模型的一种方法。

def get_interesting_queryset(self):
    for field in Manors._meta.fields:
        if field.value_to_string(self) is None or field.value_to_string(self) == "N" or field.value_to_string(self) == "0" or field.value_to_string(self) == "-" or field.value_to_string(self) == "X" or field.value_to_string(self) == "":
            delattr(self, str(field.name))
    return self
最佳回答

根据你使用的Django版本,你应当使用“in”经营者。 让我发挥你的榜样作用。 编制一份清单。

def get_field_list(self):
    interesting_fields = []
    for field in Manors._meta.fields:
        if field.value_to_string(self) != "N" and field.value_to_string(self) != "0" and field.value_to_string(self) != "-" and field.value_to_string(self) != "X":
            interesting_fields[] = field.value_to_string(self)
    return interesting_fields

然后。 在您的模板中,以下做法是:

{% if  hills  in manor.get_field_list %}<li>Hills blah blah: {{ manor.hills }}</li>{% endif %}

还指出,一旦你提出询问,国际排雷中心就可加以编辑。 因此,

for manor in Manors.objects.all():
    for field in Manors._meta.fields:
        if field.value_to_string(manor) == "N" or field.value_to_string(manor) == "0" or field.value_to_string(manor) == "-" or field.value_to_string(manor) == "X":
            delattr(manor, field.value_to_string(self))
问题回答

......

{% for k,v in manor.get_field_dictionary %}
  {% ifequal k "hills" %}
    <li>Hills blah blah: {{v}}</li>
  {% endifequal %}
{% endfor %}

假设模型方法是行之有效的,这应当使你们重新思考。





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签