不同数据类型的 Django admin list_display 为 null
Django admin list_display of different data types with null
我知道我对标题的解释很差,看我有一个简单的聊天应用程序,用户可以在其中发送 text/audio/video/image 并让我呈现这些消息我有一个方法可以检查消息的类型并呈现它因此,如果它是文本,那么我将在我的模板中将安全设置为 False,否则我将显示函数将给我的内容,即 HTML 代码
我真正想要的是:管理面板给我消息文本[:50] 如果它是文本,如果它是音频那么我可以预览它的音频,如果它是图像或视频或文件那么它会给我 url。
有办法吗?
这是我的文件,这样你可以更好地理解我在说什么:
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',)
您可以尝试使用一个名为 get_content()
的函数并将其包含在 list_display
中,例如
list_display = ('user','room', 'user', 'is_read', 'get_content')
在您的 class
中安装此功能
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 中 class。
我知道我对标题的解释很差,看我有一个简单的聊天应用程序,用户可以在其中发送 text/audio/video/image 并让我呈现这些消息我有一个方法可以检查消息的类型并呈现它因此,如果它是文本,那么我将在我的模板中将安全设置为 False,否则我将显示函数将给我的内容,即 HTML 代码
我真正想要的是:管理面板给我消息文本[:50] 如果它是文本,如果它是音频那么我可以预览它的音频,如果它是图像或视频或文件那么它会给我 url。 有办法吗?
这是我的文件,这样你可以更好地理解我在说什么:
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',)
您可以尝试使用一个名为 get_content()
的函数并将其包含在 list_display
中,例如
list_display = ('user','room', 'user', 'is_read', 'get_content')
在您的 class
中安装此功能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 中 class。