简体   繁体   English

Django处理模型字段

[英]Django dealing with a model fields

I'm new to Django and I'm trying to learn as I go. 我是Django的新手,我正在尝试学习。 And I've ended up in a situation where I can't figure out what is the best way forward. 我最终陷入了无法确定最好的前进方向的情况。

snippet from models.py: 来自models.py的摘要:

class ProjectMeta(models.Model):
    project = models.ForeignKey(Project)
    architect = models.CharField(max_length=200)
    landscape = models.CharField(max_length=100, blank=True)
    engineer = models.CharField(max_length=200, blank=True)
    client = models.CharField(max_length=100)
    consultant = models.CharField(max_length=100, blank=True)
    size = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
    location = models.CharField(max_length=200)
    date = models.DateField()

    STATUS = (
        ('CP', 'Competition'),
        ('UC', 'Under construction'),
        ('CO', 'Completed'),
    )
    status = models.CharField(max_length=2, choices=STATUS, default=1)

And this is the view: 这是视图:

class ProjectDetailView(DetailView):
    model = Project

    def get_context_data(self, **kwargs):
        context = super(ProjectDetailView, self).get_context_data(**kwargs)
        context['projectmeta_list'] = ProjectMeta.objects.all()
        return context

But if I want to output ProjectMeta in the template I could iterate over projectmeta_list . 但是,如果我想在模板中输出ProjectMeta,则可以遍历projectmeta_list

{% for metadata in projectmeta_list %}
<p>Architect: {{ metadata.architect }}</p>
{% endfor %}

But this require alot of repeating myself, and I wont work. 但是,这需要大量重复我自己,而且我不会工作。 Because lets say the architect field is empty, I would get Archiect: printed to the page. 因为可以说Architect字段为空,所以我将Archiect:打印到页面上。 Is there a built-in way of converting a model into a dict or list, so I can iterate over it and only print out fields that aren't empty to the page? 是否有将模型转换为字典或列表的内置方法,所以我可以对其进行迭代,仅打印出页面中不为空的字段?

I've been looking at get_fields(), would that work? 我一直在看get_fields(),行得通吗? https://docs.djangoproject.com/en/1.10/ref/models/meta/#retrieving-all-field-instances-of-a-model https://docs.djangoproject.com/en/1.10/ref/models/meta/#retrieving-all-field-instances-of-a-model

I tried this in the shell, threw me and AttributeError: 我在外壳中尝试过,然后抛出AttributeError:

>>> from projects.models import *
>>> Project._projectmeta.get_fields()

您应该尝试将<p>Architect: {{ metadata.architect }}</p>包裹在有条件的{% if metadata.architect != '' %}或达到该效果的某些条件中。

Try with another ProjectMeta model. 尝试使用另一个ProjectMeta模型。 Take a look at this one. 看一看。

class ProjectMeta(models.Model):
    project = models.ForeignKey(Project)
    name = models.CharField(max_length=50)
    value = models.TextField()

And this query should work. 并且此查询应该工作。 myproject.projectmeta_set.filter(name="status")

You can use built-in default or default_if_none template filters to show a default value if it is None or empty string . 如果为None或为empty string ,则可以使用内置的defaultdefault_if_none模板过滤器显示默认值。

{% for metadata in projectmeta_list %}
<p>Architect: {{ metadata.architect|default:"-" }}</p>
{% endfor %}

Check this for more details. 检查以获取更多详细信息。

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

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