【问题标题】:Django admin list_display of different data types with null不同数据类型的 Django admin list_display 为 null
【发布时间】:2021-02-02 20:38:40
【问题描述】:

我知道我对标题的解释很糟糕,看我有一个简单的聊天应用程序,用户可以在其中发送文本/音频/视频/图像并让我呈现这些消息我有一个检查消息类型并呈现它的方法因此,如果它是文本,那么我将在模板中将 safe 设置为 False,否则我将显示该函数将为我提供的 HTML 代码

我真正想要的是:管理面板给我消息 text[:50] 如果是文本,如果是音频,那么我可以对其进行音频预览,如果是图像、视频或文件,那么它将给出我的网址。 有什么办法吗?

这是我的文件,因此您可以更好地理解我在说什么:

models.py:

class Message(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    room = models.ForeignKey(Room, on_delete=models.CASCADE)

    text = models.TextField(blank=True, null=True)
    video = models.FileField(upload_to="chat/room/vid", blank=True, null=True)
    image = models.FileField(upload_to="chat/room/img", blank=True, null=True)
    file = models.FileField(upload_to="chat/room/file", blank=True, null=True)
    audio = models.FileField(upload_to="chat/room/aud", blank=True, null=True)

    is_read = models.BooleanField(default=False)
    date = models.DateTimeField(auto_now_add=True)

    def content(self):
        if self.text:
            return self.text
        elif self.video:
            return f"""
            <video width="320" height="240" controls>
                        <source src="{self.video.url}" type="video/mp4">
                        <source src="{self.video.url}" type="video/mov">
                        <source src="{self.video.url}" type="video/wmv">
                        <source src="{self.video.url}" type="video/avi">
                        <source src="{self.video.url}" type="video/avchd">
                    Your browser does not support the video tag.
                    </video> 
            """
        elif self.image:
            return f'<img src="{self.image.url}" alt="">'
        elif self.file:
            return f'<p><a href="{self.file.url}" download><i class="fas fa-download"></i> {self.filename()}</a></p>'
        elif self.audio:
            return f"""
            <audio controls>
                        <source src="{self.audio.url}" type="audio/pcm">
                        <source src="{self.audio.url}" type="audio/wav">
                        <source src="{self.audio.url}" type="audio/aiff">
                            <source src="{self.audio.url}" type="audio/mp3">
                                <source src="{self.audio.url}" type="audio/aac">
                                <source src="{self.audio.url}" type="audio/ogg">
                                <source src="{self.audio.url}" type="audio/flac">
                                <source src="{self.audio.url}" type="audio/wma">
                    Your browser does not support the audio element.
                    </audio>
            """
        else:
            return self.text

这是我的 HTML 模板:

{% for room_message in room_messages %}
    {% if room_message.text %}
    <p>{{ room_message.content }}</p>
    {% else %}
    {{ room_message.content|safe }}<br>
    {% endif %}
{% endfor %}

现在问题出在哪里?在我的管理面板中,如果没有文本,它会给我“-”作为消息内容

我的 admin.py:

from django.contrib import admin
from .models import Room, Area, Message


admin.site.register(Room)
admin.site.register(Area)
@admin.register(Message)
class MessageAdmin(admin.ModelAdmin):
    '''Admin View for Message'''

    list_display = ('user','room', 'user', 'is_read', 'text')
    readonly_fields = ('date',)

【问题讨论】:

  • 看起来您在list_display 中设置了text,但没有为videoaudio 等设置其他字段。您可以尝试包括这些字段,看看会发生什么?
  • 当我这样做时,管理页面变得非常不干净,我有 4 列并排在一起,只使用了其中的一个,我试图找到一种更清洁的方式,音频也让我下载链接而不是播放它,但图像和视频还可以

标签: python html django django-admin django-admin-actions


【解决方案1】:

您可以尝试创建一个名为 get_content() 的函数并将其包含在 list_display 中,例如

list_display = ('user','room', 'user', 'is_read', 'get_content')

在你的类中有这个函数

def get_content(self, obj):
    if obj.text:
        return obj.text
    elif obj.video:
        return obj.video.url
    elif obj.image:
        return obj.image.url
    elif obj.file:
        return obj.file.url
    elif obj.audio:
        return obj.audio.url

此方法必须在您的 ModelAdmin 类中。

【讨论】:

  • 不是真的,但我会花一些时间调试,直到找到解决方法,问题是“self”不会立即接收消息,而是需要 message.MessageAdmin 实例。但非常感谢您的帮助
  • 唯一缺少的是能够获取“自我”,无论如何要获取实例消息ID然后进行过滤来获取它?
猜你喜欢
  • 1970-01-01
  • 2019-07-28
  • 2019-06-13
  • 2020-12-29
  • 2021-06-06
  • 2019-05-13
  • 2017-08-25
  • 2013-04-28
  • 2016-01-05
相关资源
最近更新 更多