私はDjangoにかなり慣れておらず、PHPの世界から来ています。計算後にクエリセットにフィールドを「追加」しようとしていますが、その方法がわかりません。PHPでは、配列に列を追加して、そこに自分のものを格納するだけです。
これが私のコードです:
def (id):
mystuff_details = mystuff_details.objects.filter(stuff_id=id)
newthing = '';
for mystuff in mystuff_details:
newthing_lists = //some query to get another queryset
for newthing_list in newthing_lists:
newthing = newthing_list.stuffIwant
//Here I want to make some computation, and ADD something to newthing, let's say:
to_add = (mystuff.score+somethingelse)
//I've heard about the .append but I'm sure I'm screwing it up
newthing.append(to_add)
したがって、基本的に私のテンプレートでは、次のように呼び出すことができます。{%for newthing in newthings_list%} {{newthing.to_add}} {%end%}
TL; DR:基本的に、データベースからデータのリストを取得したいので、このオブジェクトのリストに、計算値を含むフィールドを追加します。
不明な点がある場合はお知らせください。phpからdjangohahaに切り替えるのに苦労しています。
ありがとう!
編集:
だから、私は辞書で試していますが、ロジックが欠けているに違いありません:
def (id):
mystuff_details = mystuff_details.objects.filter(stuff_id=id)
newthing = {};
for mystuff in mystuff_details:
newthing_lists = //some query to get another queryset
for newthing_list in newthing_lists:
//Newthing_list can have several times the same I, and the scores need to add up
if newthing[newthing_list.id] > 0: //This doesn't seem to work and throws an error (KeyError)
newthing[newthing_list.id] = newthing[newthing_list.id] + some_calculated_thing
else:
newthing[newthing_list.id] = some_calculated_thing
そして、それが機能するようになると、テンプレートでそれにアクセスする方法がわかりません。
{% for id in my_list %}
{{newthing[id]}} ? Or something like newthing.id ?
{% end %}
ありがとう!