0

私はこのような2つのモデルを持っています:

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_overview = models.IntegerField()
    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()

ファイルを共有している人を数えたい。たとえば、ファイルok.txtが2人で共有されている場合、file_informationを表示し、shared_withcoloumn2の値を表示します。次のようにします。

    file_s = Share.objects.filter(users_id=log_id)
 shared_with = Share.objects.filter(files_id__in = file_s).values_list('shared_user_id', flat=True).distinct().count() 

ただし、各ファイルに同じshared_with番号が表示されています。私は何が間違っているのですか?

編集:実際には私のクエリは正しいですが、テンプレートでこれを呼び出す方法がわかりませんでした:

#views.py
def shared_by_me(request):
    try: 
        log_id = request.user.id
        username = request.user.username

        #Shared by me file information
        file_s = Share.objects.filter(users_id=log_id)

        b = [i.files_id for i in file_s]
        c = map(str, b)
        c = ''.join(c)
        shared_with = Share.objects.filter(files_id__in = file_s).values_list('shared_user_id', flat=True).distinct().count() 
        shared_file = File.objects.filter(id__in= Share.objects.filter(users_id = log_id).values_list('files', flat=True))
        #shared_username = User.objects.filter(id__in= Share.objects.filter(users_id = log_id).values_list('shared_user_id', flat=True))
        shared_username = Share.objects.filter(shared_user_id=log_id)
        #shared_file = File.objects.filter(id__in = c, users__id = log_id)
        return render_to_response('shared_by_me.html', {'shared_by_me':shared_file, 'shared_with':shared_with, 'username':username, 'shared_username':shared_username}, context_instance=RequestContext(request))
    except ValueError:
        return render_to_response('shared_by_me.html', {'username':username}, context_instance=RequestContext(request))

#template:
    {% for choice in shared_by_me %}
            <tr class="oddclass">
              <td><input type="checkbox" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" onchange="checkChecked()"/></td>
              <td><label for="choice{{ forloop.counter }}">{{ choice.file_name }}</label></td>
              <td>{{ choice.type }}</td>
              <td>{{ choice.size }}</td>
              <td>{{ choice.end_date }}</td>
              <td><a href="#" onclick="share()">{{ shared_with }}</a></td>
              {% endfor %}

            </tr>
4

1 に答える 1

0

多分これを試してみませんか?

file_s = Share.objects.filter(files__pk = somePk).count()

すべての共有オブジェクトは 1 人のユーザーのみに対応するため、主キーを持つShareオブジェクトの数がわかります。filessomePk

そしてcount()数を数えるだけです

于 2013-02-08T04:36:38.833 に答える