django - Form instantiation mechanism -
i have problem django form instantiation. sum up, here's code. in view, have generate context related many objects.
def my_view(request): myobjects = objectfrommodels.objects.filter(some_attribute = '1234') item in myobjects: form_related = associatedform(item_pk = item.pk) my_context[item] = form_related class associatedform(forms.form): # attributes def __init__(self,item_pk,*args,**kwargs) super(associatedform,*args,**kwargs) if item_pk : obj = objectfrommodels.objects.get(pk=item_pk) self.someattribute = obj.calling_function() # returns list
here's trouble, in view, after iteration, items, value of my_context[item.name]
overriden value of last element of iteration
in order debug, printed results :
def my_view(request): myobjects = objectfrommodels.objects.filter(some_attribute = '1234') item in myobjects: form_related = associatedform(item_pk = item.pk) print(form_related.someattribute) my_context[item.name] = form_related key,val in my_context: print(key) print(val.someattribute)
let's take example. filter function returns 3 objects : [objecta,objectb,objectc]
and calling calling_function()
these objects returns :
- for item : ['aze','zer','ert']
- for item b : ['qsd','sdf','dfg']
- for item c : ['wxc','xcv','vbn']
logically, after iterations, have in context. thing that, returns :
{<objecta>:['wxc','xcv','vbn'], <objectb>:['wxc','xcv','vbn'], <objectc>:['wxc','xcv','vbn']}
i first though problem of reference both forms in context have different memory addresses. tried deep_copy
nothing changed.
i suspect consider 3 different forms 1 form can't explain why because said, both have different memory addresses. , why, in loop, display of form attributes right outside loop, messing , parsing last dict both of entries of context dict ?
does have clue identify problem ?
Comments
Post a Comment