如何将几条记录的内容合二为一?

时间:2018-02-06 08:30:52

标签: django django-admin

如何将几条记录的内容显示为一个???

我必须做那样的事情

enter image description here

1 个答案:

答案 0 :(得分:0)

型号:

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")