用文件过滤字段

时间:2018-06-28 06:57:37

标签: python django

我只想获取带有文件的歌曲

models.py

这包括歌曲模型

song_genre = (
    (1,"Jazz"),
    (2,"Pop "),
    (3,"Rock"),
    (4,"Hip Hop / Rap"),
    (5,"R&B / Soul"),
    )
class Songs(models.Model):
    name = models.CharField(max_length=1000,blank=True, null= True)
    genre = models.CharField(choices=song_genre)
    file = models.FileField()

    def __str__(self):
        return str(self.name)

views.py

我尝试使用file__isnull过滤器,但模板为空,但示例数据中包含没有歌曲的文件(歌曲文件)

from .models import Song

def song_with_file(request):
    songs = Song.objects.filter(file__isnull=True)

    return request(render, 'song_with_file.html', {'songs':songs})

templates / song_with_file.html

{% include 'base.html' %}

{% block content %}

{% for song in songs%}
  {{song}}
{% endfor %}

{% endblock %}

0 个答案:

没有答案