On Sunday, January 30, 2011 6:01:00 PM UTC, a.esmail wrote:
>
> Hello, 
> I tried my best with no luck. 
> Here are the relevant code snippets: 
>
> <snip>
>
> ################################################# 
> Item form: 
> class ItemForm(forms.ModelForm): 
>     def __init__(self, fields_list, *args, **kwargs): 
>         super(ItemForm, self).__init__(*args, **kwargs) 
>         for field in self.fields: 
>             if field not in fields_list: 
>                 field_name = str(field) 
>                 del self.fields[field_name] 
>
>     class Meta(): 
>         model = Item 
>
> ################################################# 
>
> I know this is not the best way to do this, but I just want to 
> understand why this happens. 
> For some reason, the for loop in ItemForm doesn't loop through all the 
> fields. 
> It only goes through 8, sometimes 9 fields out of the 14 in 
> self.fields. (e.g., I can't see the activation_code field in the 
> loop). 
> I printed self.fields and it showed all the fields in the Item model, 
> so why don't some of them show up in the loop? 
> I want to filter the fields based on the category chosen. The 
> fields_list parameter is a list of strings representing the fields I 
> want to include in the final form. 
>
> Any help would be appreciated. I've spent more hours than I can count 
> on this. 
> Thank you.


You're modifying a list while iterating through it. Don't do that. Make a 
copy of the list and use that instead. Or, even better:
    self.fields = [f for f in self.fields if str(f) in fields_list]
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to