django模型文件字段上传

时间:2017-07-05 06:10:25

标签: django python-3.x django-models

我想创建一个歌曲列表,例如“电影名称泰坦尼克号,它包含所有歌曲”,但我不知道在电影中添加多首歌曲,有人可以帮助我吗?

这就是我正在做的事情

views.py

from django.shortcuts import render_to_response
from .models import Songs

def Songs(request):
    songs = Songs.objects.all()
    return render_to_response('profile_page.html',{'songs':songs})

models.py

from django.db import models

class Movie(models.Model):
    name= models.CharField(max_length = 100)

class Songs(models.Model):
    movie = models.ForeignKey(Movie, default= 1)
    song_title = models.CharField(max_length=20)
    song_poster = models.FileField()
    song_list = models.FileField()

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^',views.Songs),
]

1 个答案:

答案 0 :(得分:0)

创建一个名为Movie

的模型
class Movie(models.Model):
    name= models.CharField(max_length = 100)
    // other fields as needed

class Songs(models.Model):
    movie = models.ForeignKey(Movie)
    song_title = models.CharField(max_length=20)
    song_poster = models.FileField()
    song_list = models.FileField()

然后根据电影ID和查询添加歌曲