2012-08-25 11 views
6

मैं 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 %} 

धन्यवाद!

उत्तर

3

शब्दकोश का उपयोग क्यों नहीं करें?

newthing = {} 
newthing['your_key'] = to_add 

टेम्पलेट में, आप के साथ मूल्यों शब्दकोश पहुँच सकते हैं:

{{newthing.your_key}} 

या पाश के लिए उपयोग करें यदि आप शब्दकोशों

+0

हैलो और धन्यवाद! मैंने उपन्यासों के साथ प्रयास किया, यह मेरे प्रश्न में "संपादन" के बाद हिस्सा है, लेकिन अभी तक कोई भाग्य नहीं है। मैंने अपने कोड में क्या गलत किया है? धन्यवाद! –

+0

सबसे पहले, [django में लेखन विचार] देखें (https://docs.djangoproject.com/en/dev/topics/http/views/)। आपके विचार में, आपको 'वापसी render_to_response (' my_template.html ', my_data_dictionary, context_instance = RequestContext (अनुरोध)) ' – smang

22

आप एक अजगर वस्तु पर कुछ भी सेट कर सकते हैं का एक शब्दकोश है:

for obj in self.model.objects.all() : 
    obj.score = total_score/total_posts 

यह तब भी काम करेगा जब ओबीजे के पास स्कोर विशेषता नहीं है। टेम्पलेट अनुरोध में ऐसा है:

{{ obj.score }} 

हाँ, यह इतना आसान है। हालांकि, यदि आप जो गणना कर रहे हैं वह डेटाबेस में किया जा सकता है, तो आपको annotate पर देखना चाहिए।

+0

मुझे पता है कि कुछ लोग कह सकते हैं कि यह बहुत आसान है, लेकिन शायद यह है मैंने इसके लिए सबसे अच्छा सरल समाधान देखा है। –

+0

सरल और सर्वोत्तम समाधान !! धन्यवाद @Melvyn – javed