数据库 – (1242,’子查询返回超过1行’)Django中的错误?

前端之家收集整理的这篇文章主要介绍了数据库 – (1242,’子查询返回超过1行’)Django中的错误?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Django中有这样的模型:
class File(models.Model):
    users = models.ForeignKey(User)
    file_name = models.CharField(max_length=100)
    type = models.CharField(max_length=10)
    source = models.CharField(max_length=100)
    start_date = models.TextField()
    end_date = models.TextField()
    duration = models.TextField()
    size = models.TextField()
    flag = models.TextField()
    #delete_date = models.CharField(max_length=100,null=True,blank=True)

class Share(models.Model):
    users = models.ForeignKey(User)
    files = models.ForeignKey(File)
    shared_user_id = models.IntegerField()
    shared_date = models.TextField()

我正在尝试提取登录用户共享的文件.我只是在Share中查询

file_s = Share.objects.filter(users_id=log_id)

这将提取登录用户共享的文件.因为,现在我知道登录用户共享了哪个文件,我试图从文件表中获取文件信息:

shared_file = File.objects.filter(users_id=file_s)

但这是回归:

DatabaseError at /shared_by_me/
(1242,'Subquery returns more than 1 row')

#my_views
def shared_by_me(request):
    log_id = request.user.id
    username = request.user.username
    #shared_file = File.objects.filter(users_id=file)
    file_s = Share.objects.filter(users_id=log_id)
    shared_file = File.objects.filter(users_id=file_s)
    #b = Share.objects.filter(users_id=log_id)
    return render_to_response('shared_by_me.html',{'shared_by_me':shared_file,'username':username},context_instance=RequestContext(request))

#my_template
{% for choice in shared_by_me %}
            <tr class="oddclass">
              <td><input type="checkBox" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" /></td>
              <td><label for="choice{{ forloop.counter }}">{{ choice.file_name }}</label></td>
              <td>{{ choice.type }}</td>
              <td>{{ i.size }}</td>
              <td>{{ i.end_date }}</td>
            </tr>
{% endfor %}

我究竟做错了什么?

解决方法

因为文件不是模型而是查询集,所以你应该使用__in,例如:
shared_file = File.objects.filter(users_id__in=file_s)
原文链接:https://www.f2er.com/mssql/75960.html

猜你在找的MsSQL相关文章