2

私はそのようにセットアップしました(簡単にするために変更しました)

class Author(models.Model)
    name = models.CharField(max_length=100)
    ...

class Document(models.Model):
    title = models.CharField(max_length=200)
    content - models.TextField()
    author = models.ForeignKey("Author", related_name="documents")
    date_published = models.DateTimeField()
    categories = models.ManyToManyField("Category")

class Category(models.Model):
    name = models.CharField(max_length=100)

Author レコードを取得していますが、特定の条件 (date_published とカテゴリなど) に一致する各作成者の関連ドキュメント レコードのみを取得したいと考えています。

これを行う簡単な方法は、 を使用してレコードを辞書のリストとしてAuthor.objects.values()取り込み、各レコードをループして実行することです。

author['documents']=Document.objects.filter(categories__in=[category_list], date_published__year=year)`

ただし、これは django-piston 用に生成されたものであり、QuerySet オブジェクトを返すと (特に、独自のフィールドを定義している場合は!) 一番うまくいくようです。

これの一部は、ベースの django-piston コードに変更を加えたためかもしれません。基本的に、ここのコードの現在のバージョンは値を上書きしfieldsます。このコードを変更fieldsして、リクエストに基づいて Handler の値を変更できるようにしました (そのため、リクエストが特定のリソースに対するものである場合に詳細を提供できます)。

したがって、私の質問は3つあると思います。

  1. documentsレコードのサブレコードをフィルタリングまたは何らかの方法で制限する方法はありますか (つまり、各レコードをフィルタリングするauthor.documents)
  2. そうでない場合、django-pistonでも機能するこれを行う機能的な方法は何ですか?
  3. 私がやろうとしていることを行うためのより簡単でより良い方法はありますか (ID が指定されていない場合はドキュメントなしですべての作成者を表示しますが、1 人の作成者のみにフィルタリングする場合はサブレコードを表示します)?

明確化

さて、明確にするために、ここに私が欲しい疑似コードがあります:

def perhaps_impossible_view(request, categories=None, year=None):
    authors = Author.objects.all()
    authors.something_magical_happens_to_documents(category__in=[categories], date_published__year=year)
    return render_to_response('blar.html', locals(), RequestContext(request))

テンプレートに入れると、これ変更なしで機能します。

{% for a in authors %}
    {% for d in authors.documents.all %}
        {{ d.title }} is almost certainly in one of these categories: {{ categories }} and was absolutely published in {{ year }}. If not, .something_magical_happens_to_documents didn't work.
    {% endfor %}
{% endfor %}

something_magical_happens_to_documentsdocumentsを実行して、各著者レコードの内容を実際に変更する必要があります。これは可能であるように思われますが、おそらくそうではありませんか?

4

1 に答える 1

0

編集...またはより良い...置き換え!:)

確かに、ドキュメントが一致しない作成者はクエリセットに含まれないため、後で追加する必要があります (より良い方法は見つかりませんでしたが、生の SQL を使用せずに作成者を削除しない方法を知っている人がいるかもしれません)。

クエリセットを使用してドキュメント数を取得しないため、作成者の完全なドキュメント数を取得できます。

qryset = Author.objects.all()
qryset = qryset.filter(documents__categories__name__in = category_list).distinct()
qryset = qryset.filter(documents__date_published__year=year)

print(qryset)与えられ[<Author: Author object>](1人の作成者のみがすべてのカテゴリに一致した場合)、一致したドキュメントの数のみqryset[0].documents.count()が返されます(作成者からのすべてのドキュメントではありません-私がテストした場合、作成者には4つありましたが、すべての条件に一致するのは2つだけでした)。

上記の手順でクエリセットの代わりに dict (.values()) を使用する場合、dict にはドキュメント フィールドがないため、それを行うことはできません (私はそう思います)。

qryset_dict = Author.objects.values()
qryset_dict = qryset_dict.filter(documents__categories__name__in = category_list).distinct().values()
qryset_dict = qryset_dict.filter(documents__date_published__year=year).values()

を発行qryset_dict[0].documents.count()すると、次のエラーが表示されます。

AttributeError: 'dict' object has no attribute 'documents'


フィルタリングされた著者を元に戻すには、次のようにします。

res = []
for a in Author.objects.all():
    if a in qryset:
        res.append([a,a.documents.count()])
    else:
        res.append([a,0])

res は<authors>、1 列目にリストがあり、2 列目に一致するドキュメントの数があります。

私はこれが完璧とはほど遠いことを知っています...しかし、著者ごとの一致するドキュメントのみに興味がある場合は、djangoの集計と注釈を使用するより良い方法を見つけるか、作成者からドキュメントへのを使用して生のSQLで作成count()できると思います.left join




問題の説明後の編集:

def possible_view(request, categories=None, year=None):
    # you will pass these as parmeters of course
    category_list = ['c2', 'c3']
    year = 2010

    qryset = Document.objects.filter(categories__name__in = category_list).distinct()
    qryset = qryset.filter(date_published__year=year)
    authors = Author.objects.all()
    return render_to_response('blar.html', { 'result': qryset, 'authors': authors, 'categories': category_list, 'year': year }, RequestContext(request))

テンプレートblar.html:

{% for a in authors %}
    <b>{{a.name}}</b><br />
    {% for d in result %}
        {% if d.author.name == a.name %}
            {{ d.title }} is almost certainly in one of these categories: {{ categories }} and was absolutely published in {{ year }}. If not, .something_magical_happens_to_documents didn't work.<br />
        {% endif %}
    {% endfor %}<br />
{% endfor %}

これにより、あまりきれいではありませんが、すべての作成者と各作成者の下に、category_list(OR 条件、AND の場合は、を使用する代わりに各カテゴリのクエリをフィルター処理する必要があります) のいずれかに該当するドキュメントのリストが表示されます__in

作成者が にドキュメントを持っていない場合は、そのcategory_list下にドキュメントなしでリストされます。


何かのようなもの:

aut1
tit2 is almost certainly in one of these categories: ['c2', 'c3'] and was absolutely   published in 2010. If not, .something_magical_happens_to_documents didn't work.
tit1 is almost certainly in one of these categories: ['c2', 'c3'] and was absolutely published in 2010. If not, .something_magical_happens_to_documents didn't work.

aut2
tit3 is almost certainly in one of these categories: ['c2', 'c3'] and was absolutely published in 2010. If not, .something_magical_happens_to_documents didn't work.

aut3
于 2010-09-17T23:19:07.770 に答える