在Djang中检索对象时出现AttributeError

2024-04-20 00:15:46 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的webapp上下载的每一部电影,我都要通过邮件通知上传者他的电影已经下载了。我写了下面的代码,似乎我发现很难得到正确的查询。你知道吗

  AttributeError at /download/

 'unicode' object has no attribute 'email'

型号:

class Emovie(models.Model):
    User=models.ForeignKey(User)
    movie_file=models.FileField(upload_to='miiv')
    movie_name=models.CharField(max_length=50)
    email=models.EmailField()   #email of the uploader
    #other fields follows

你知道吗视图.py你知道吗

@login_required
def document_view(request, emovie_id, urlhash):
    document=Emovie.objects.get(id=emovie_id, urlhash=urlhash)
    downstats=Emovie.objects.filter(id=emovie_id, urlhash=urlhash).update(download_count=F('download_count')+1)
    #where email notification starts
    notify=Emovie.objects.filter(id=emovie_id.email)
    send_mail('Your file has just been downloaded','this works','test@test.com',[notify])
    response = HttpResponse()
    response["Content-Disposition"]= "attachment; filename={0}".format(document.pretty_name)
    response['X-Accel-Redirect'] = "/protected_files/{0}".format(document.up_stuff)
    return response

我该怎么办?你知道吗


Tags: idobjects电影modelsemailresponsedownloadmovie
2条回答

错误发生在这里:Emovie.objects.filter(id=emovie_id.email)。你知道吗

emovie_id是一个unicode(string)对象,这意味着您不能对它执行.email。既然你想在id=上过滤,我想你是想写:Emovie.objects.get(id=int(emovie_id))。你知道吗

替换

notify=Emovie.objects.get(id=emovie_id).email

notify=Emovie.objects.filter(id=emovie_id.email)

会很好的。。。你知道吗

相关问题 更多 >