Reputation: 1848
I have a model with a manytomany column. I want to limit the number of groups a user can submit.
Here is what I'm doing
group_name = [g.id for g in form.cleaned_data['groups']]
gr=group_name.id.count()
if gr>3:
raise forms.ValidationError("you can only add 3 groups")
return group_name
the error is:
Exception Value: 'list' object has no attribute 'id'
I can't seem to find what I'm doing wrong. Thanks.
Upvotes: 0
Views: 2180
Reputation: 43840
You're creating a list
object, so standard list
operations work.
group_name_ids = [g.id for g in form.cleaned_data['groups']]
group_id_count = len(group_name_ids)
It seems that "groups"
here is a list of model objects, so you could get the count via:
group_count = len(form.cleaned_data["groups"])
However, since your returning the resulting built group_name_ids
list, you might as well just get the len()
of that.
Upvotes: 2
Reputation: 6957
>>> nk=['city1','city2','city1','city3','city1']
>>> from collections import Counter
>>> mycount=Counter()
>>> for word in nk:
... mycount[word]+=1
...
>>> mycount['city1']
3
>>>
refer python documentaion http://docs.python.org/library/collections.html
Upvotes: 0
Reputation: 7198
group_name
is a list, and group_name.id
cannot be resolved :)
Upvotes: 0