python - 不同数据类型的Django admin list_display与null
问题描述
我知道我对标题的解释很糟糕,看我有一个简单的聊天应用程序,用户可以在其中发送文本/音频/视频/图像,并为我呈现这些消息,我有一个方法可以检查消息的类型并相应地呈现它,如果它是文本,那么我将在我的模板中将 safe 设置为 False,否则我将显示该函数将为我提供的 HTML 代码
我真正想要的是:管理面板给我消息 text[:50] 如果是文本,如果是音频,那么我可以对其进行音频预览,如果是图像、视频或文件,那么它将给我 url . 反正有这样做吗?
这是我的文件,因此您可以更好地理解我在说什么:
模型.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 %}
现在问题出在哪里?在我的管理面板中,如果没有文本,它会给我“-”作为消息内容
我的管理员.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',)
解决方案
您可以尝试调用一个函数get_content()
并将其包含在list_display
like
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 类中。
推荐阅读
- javascript - RegEx JavaScript 替换等号后跟一个新行
- visual-studio-code - 如何仅更改侧边栏的主题?
- python - 如何在一台机器上运行 gRPC 客户端,在另一台机器上运行 gRPC 服务器?
- django - 序列化具有多对多字段的嵌套 Django 模型
- python - 如何提取 DataCompy 比较结果?
- python - 我们如何将模型设置为 django 中的 default=0 或 default=null
- spring - 您应该如何对 HATEOAS 和返回 HATEOAS 的控制器进行单元测试?
- uipath - Uipath如何从不同的excel表中添加数据
- python - 如何按键值正确排序字典?
- python-3.x - 我想清理一个脚本,它会在 X 分钟不活动后自动锁定并关闭我的屏幕