Reputation: 2197
Cant find out why I keep getting following following error:
TypeError at /boats/reversion/ count() takes 1 positional argument but 2 were given
#views
…
…
…
memory_limiter = BoatImage.objects.filter(boat_id__isnull=True).exclude(
memory__in=existing_boats_pk).values_list("memory", flat=True)
for i in memory_limiter:
if memory_limiter.count(i) > 3:
memory_limiter.remove(i)
what shell says about memory limiter:
>>> for memory in memory_limiter:
... print(memory, type(memory))
...
93 <class 'int'>
93 <class 'int'>
93 <class 'int'>
93 <class 'int'>
93 <class 'int'>
93 <class 'int'>
93 <class 'int'>
100 <class 'int'>
102 <class 'int'>
102 <class 'int'>
102 <class 'int'>
>>> memory_limiter
<QuerySet [93, 93, 93, 93, 93, 93, 93, 100, 102, 102, 102]>
>>>
following method brings same results:
for i in [ x for x in memory_limiter]:
if memory_limiter.count(i) > 3:
memory_limiter.remove(i)
Question is whats wrong with the count? I copy pasted this list to a separate *.py
module and there it works fine…
Tried to turn qs to iterator -same results.
in separate module
a = [93, 93, 93, 93, 93, 93, 93, 100, 102, 102, 102]
for i in a:
if a.count(i) > 3:
a.remove(i)
print(a)
----- >[93, 93, 93, 100, 102, 102, 102]
Upvotes: 0
Views: 94
Reputation: 1261
Nothing is wrong with django here.
memory_limiter = BoatImage.objects.filter(boat_id__isnull=True).exclude(
memory__in=existing_boats_pk).values_list("memory", flat=True) # This returns QuerySet object.
for i in memory_limiter:
# QuerySet class has instance method .count(), but you can not provide parameters. Also, QuerySet does not support remove() method.
# if memory_limiter.count(i) > 3:
print(memory_limiter.count())
Your code works on list
. But Django's QuerySet
and python's list
are different objects, with different methods.
But you can do something like this. This should work(IMHO).
memory_limiter = list(BoatImage.objects.filter(boat_id__isnull=True).exclude(
memory__in=existing_boats_pk).values_list("memory", flat=True)) # You should cast QuerySet object into list, then you can operate over it as a normal python's list.
for i in memory_limiter:
if memory_limiter.count(i) > 3:
memory_limiter.remove(i)
Upvotes: 2