मैं Django के लिए काफी नया हूं और PHP दुनिया से आया हूं। मैं चीजों की गणना करने के बाद एक क्वेरीसेट में फ़ील्ड जोड़ने के लिए प्रयास कर रहा हूं, और यह नहीं जानता कि इसे कैसे किया जाए। PHP में मैं सिर्फ एक सरणी में एक कॉलम जोड़ता हूं और इसमें अपनी सामग्री संग्रहीत करता हूं। {{newthing.to_add}} { % {newthings_list% में newthing के लिए%} :Django - गणना गणना के लिए क्वेरीसेट में फ़ील्ड जोड़ें
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)
तो मेरे टेम्पलेट में मूल रूप से मैं फोन करने में सक्षम होना चाहते हैं
:
यहाँ मेरी कोड है अंत%}
टीएल; डीआर: मैं मूल रूप से अपने डेटाबेस से सामान की एक सूची पुनर्प्राप्त करना चाहता हूं, और वस्तुओं की इस सूची में एक फ़ील्ड जोड़ें जिसमें एक गणना मूल्य होगा।
मुझे अस्पष्ट होने पर बताएं, मुझे php से django haha में स्विच करने में कठिनाई हो रही है।
धन्यवाद!
संपादित करें:
तो, मैं एक dictionnary साथ कोशिश कर रहा हूँ, लेकिन मैं तर्क लापता किया जाना चाहिए:
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 %}
धन्यवाद!
हैलो और धन्यवाद! मैंने उपन्यासों के साथ प्रयास किया, यह मेरे प्रश्न में "संपादन" के बाद हिस्सा है, लेकिन अभी तक कोई भाग्य नहीं है। मैंने अपने कोड में क्या गलत किया है? धन्यवाद! –
सबसे पहले, [django में लेखन विचार] देखें (https://docs.djangoproject.com/en/dev/topics/http/views/)। आपके विचार में, आपको 'वापसी render_to_response (' my_template.html ', my_data_dictionary, context_instance = RequestContext (अनुरोध)) ' – smang