On Mar 22, 11:19 am, James <[email protected]> wrote:
> Hi,
>
> I am struggling to get a piece of code working.
> I've looked at it too much and now can't see what im doing wrong.
>
> Anyone have any ideas? The error is as follows:
> Type Error
> string indices must be integers - (if ids['m'['manufacturer__id']] is
> not m['manufacturer__id']: )
>
> Code:
>
> tmp_manufacturers = Product.objects.filter(category=category).values(
>         'manufacturer__title', 'manufacturer__slug',
> 'manufacturer__id'
>     ).distinct()
>
>     manufacturers = {}
>     for m in tmp_manufacturers:
>         ids = {}
>
>         if ids['m'['manufacturer__title']] is not
> m['manufacturer__title']:
>             ids.append(m['manufacturer__title'])
>             manufacturers.append({
>                 'title': m['manufacturer__title'],
>                 'slug': m['manufacturer__slug'],
>                 'id': m['manufacturer__id'],
>                 'total_products': Product.objects.filter(
>                     category=category,
>                     manufacturer__slug=m['manufacturer__slug']
>                 ).count()
>             })
>
> Also, does anyone have any decent documentation on arrays?

Firstly, this is not an array, or even the Python equivalent (a list),
but a dictionary. If you want documentation on these, you should look
at the excellent Python docs (eg the reference on standard types at
http://docs.python.org/library/stdtypes.html or the tutorial on
datastructures at http://docs.python.org/tutorial/datastructures.html).

Secondly, your code has at least one syntax error, but even without
those, it will always fail because of its logic errors. The initial
syntax error should be obvious in the line of code you show: you have
quotes around the 'm', ie referring to the string 'm', when you just
want to refer to the dictionary m, without quotes.

But even fixed, that line makes no sense. Firstly, do not use 'is' or
'is not' for comparisons. That checks for **identity**, which is a
completely different thing from **equality**. You should use !=
instead. And then ask yourself how that line could ever be True. You
have just set ids to a blank dictionary, so there is no such thing as
ids[anything]. That line will now always fail with an IndexError.

Then, note that both 'ids.append()' and 'manufacturers.append()' will
fail with AttributeErrors, because these are both dictionaries, not
lists, and so have no append function.

Perhaps you should explain exactly what you're trying to achieve, with
an example of the expected output, and we should be able to help in
more detail.
--
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