0

模型.py

class File(models.Model):
    name = models.CharField(max_length=255)
    src = models.FileField(upload_to="files")

class UserFile(models.Model):
    user = models.ForeignKey(User)
    files = models.ManyToManyField(File)

视图.py

def my_files(request):
    user = request.user
    myfile = UserFile.objects.filter(user=user)

    return render_to_response('myfiles.html',context_instance=RequestContext(request))

如何获取属于当前登录用户的文件?

4

1 回答 1

1
files = File.objects.filter(userfile__user=request.user)

模板只是:

{% for file in files %}{{ file.src }}{% endfor %}

查看跨越关系的查找

于 2013-06-21T18:03:12.430 回答