Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(1242, 'Subquery returns more than 1 row') error in Django?

I have a model like this in 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()

I am trying to extract the file shared by logged in user. I simply query in Share

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

This extracts the file shared by logged in user. Since, now I know which file is shared by logged in user I tried to get the file information from file table:

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

But this is returning:

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 %}

What am I doing wrong?

like image 564
user1881957 Avatar asked Jan 22 '13 08:01

user1881957


1 Answers

Because file is not a model but a queryset you should use __in, something like:

shared_file = File.objects.filter(users_id__in=file_s)
like image 182
dani herrera Avatar answered Nov 12 '22 21:11

dani herrera