简体   繁体   English

如何显示 django 中查询集中的表名

[英]How to display name of table from queryset in django

Trying to get the table name in django, I need it to display the detailview correctly via if state.net.试图获取 django 中的表名,我需要它通过 if state.net 正确显示详细视图。 I have such a view to display我有这样的观点要展示

class Home(ListView):
    template_name = 'home.html'
    def get_queryset(self):
        qs1 = Book.objects.all()
        qs2 = CD.objects.all()
        qs3 = Film.objects.all()
        queryset = sorted(list(chain(qs1, qs2, qs3)), key=operator.attrgetter('title'))
        return queryset

and it returns to me this它返回给我这个

[<CD: Music1>, <CD: Music2>, <Book: Some books>] [<CD: Music1>, <CD: Music2>, <Book: 一些书>]

How can I get "CD" or "Book" in this template如何在此模板中获取“CD”或“Book”

{% block content %}
<div class="row">
{% for object in object_list %}
    <div class="col-md-3">
        <div class="card card-product-grid">
            <img src="{{ object.image.url }}">
            <a href="{% url 'DetailBook' object.pk %}" class="title">{{ object.title }}</a>
        </div>
    </div>
{% endfor %}
</div>
{% endblock content %}

At the same time, if it's a bad idea to display detailview and listview and it's done differently, I'd appreciate it if you'd let me know同时,如果显示 detailview 和 listview 是个坏主意,而且它的做法不同,请告诉我,我将不胜感激

I tried different ways of displaying object.key in a loop but it didn't work very well.我尝试了不同的方式来循环显示 object.key,但效果不是很好。 And other queryset queries.和其他查询集查询。

I've been down the list(chain(ob1, ob2, .. obn) route. It proved highly tedious from a standpoint of code maintainability and complexity. Django Polymorphic is the way to go here.我一直在list(chain(ob1, ob2, .. obn)路线下。从代码可维护性和复杂性的角度来看,它证明非常乏味。Django 多态是通往 go 的方式。

from polymorphic.models import PolymorphicModel

class Product(PolymorphicModel):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='products')
    title = models.CharField(max_length=100)
    slug = ...
    ... more fields all products share ex: price

    def __str__(self):
        return str(self.title)

    @property
    def model_name(self):
        return self._meta.model_name

class Cd(Product):
    <model fields for CD model>

class Book(Product):
    <model fields for book model>

class Film(Product):
    <model fields for film model>

Then:然后:

Product.objects.all()

Will return the instances of all CD, Book and Film objects.将返回所有 CD、Book 和 Film 对象的实例。

In your template, you can use the property model_name to check if the object is a certain type of model.在您的模板中,您可以使用属性model_name来检查 object 是否是某种类型的 model。

{% block content %}
<div class="row">
{% for object in object_list %}
    {% if object.model_name == 'book' %}
    <div class="col-md-3">
        <div class="card card-product-grid">
            <img src="{{ object.image.url }}">
            <a href="{% url 'DetailBook' object.pk %}" class="title">{{ object.title }}</a>
        </div>
    </div>
    {% endif %}
{% endfor %}
</div>
{% endblock content %}

You can obtain this with the .verbose_name attribute [Django-doc] of the model options, so my_object._meta.verbose_name .您可以使用 model 选项的.verbose_name属性[Django-doc]获得它,所以my_object._meta.verbose_name There is however a problem here: you can not access variables that start with an underscore, since these are "protected" or "private".然而这里有一个问题:您不能访问以下划线开头的变量,因为这些变量是“受保护的”或“私有的”。

A solution might be to work with a template filter.一种解决方案可能是使用模板过滤器。 You can define a templatetags directory:您可以定义一个templatetags目录:

app_name/
    __init__.py
    models.py
    templatetags/
        __init__.py
        model_utils.py
    views.py

where you create the files in boldface.您在其中以粗体字创建文件。 In model_utils , you can construct a filter with:model_utils中,您可以构造一个过滤器:

from django import template

register = template.Library()


@register.filter
def modelname(value):
    return value._meta.verbose_name

then we can use these in the template with:然后我们可以在模板中使用这些:

{% load model_utils %}

{% for object in object_list %}
    …
    {{ object|modelname }}
{% endfor %}

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

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