【问题标题】:How to display the contents of several records into one?如何将多条记录的内容合二为一?
【发布时间】:2018-07-16 06:05:00
【问题描述】:

如何将多条记录的内容合二为一显示???

我必须这样做

enter image description here

【问题讨论】:

  • 您想在一个字段中显示多个字段(的组合)吗?是这个意思吗?
  • 你到底是什么意思?
  • 是的,我想在一个字段中显示多个字段(组合)。
  • 你能说出这些记录是从哪里来的吗?或者给我们举个例子?

标签: django django-admin


【解决方案1】:

型号:

from django.db import models


class Item(models.Model):
    """Item model."""

    title = models.CharField(
        max_length=255,
        null=True,
        blank=True
    )
    url = models.TextField(
        null=False,
        blank=False
    )
    active = models.BooleanField(
        default=False
    )

    class Meta(object):
        """Meta options."""

    def __str__(self):
        return self.title

管理员:

from django.contrib import admin
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _

from .models import Item

@admin.register(Item)
class ItemAdmin(admin.ModelAdmin):
    """Item admin."""

    list_display = (
        'id',
        'title',
        'url',
        'active',
        'combined',
    )

    def combined(self, obj):
        """Combined data.

        :return:
        """
        return format_html(
            '''
            {}<br/>
            {}<br/>
            {}
            ''',
            obj.title,
            obj.url,
            obj.active
        )

    combined.short_description = _("Combined data")

【讨论】:

  • 非常感谢您的帮助
  • 如何在模板中显示组合数据?
  • {{ obj.title }}
    {{ obj.url }}
    {{ obj.active }}
猜你喜欢
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多