Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin issue: admin/silver/provider: 'unicode' object has no attribute 'items' #634

Open
jeromecc opened this issue Feb 3, 2019 · 1 comment

Comments

@jeromecc
Copy link
Contributor

jeromecc commented Feb 3, 2019

Hello!

I am using django-silver version 0.7.0 installed through pip with Django 1.11, Python 2.7.

The admin is working well with one exception: when I try to see the list of providers in the admin, I get this error.

http://127.0.0.1:8008/admin/silver/provider/
AttributeError
'unicode' object has no attribute 'items'
/home/elkcloner/.virtualenvs/copper/local/lib/python2.7/site-packages/silver/admin.py in metadata, line 68

In template /home/elkcloner/.virtualenvs/copper/local/lib/python2.7/site-packages/django/contrib/admin/templates/admin/base.html, error at line 75
75 {% block object-tools %}{% endblock %}

Seems related to this piece of code in silver/admin.py

def metadata(obj):
    d = u'(None)'
    if obj.meta:
        d = u''
        for key, value in obj.meta.items():
            d += u'%s: <code>%s</code><br>' % (escape(key), escape(value))
    return d
metadata.allow_tags = True

The url http://127.0.0.1:8008/admin/silver/provider/1/ works normally.

A bit more debug info (i am using the default provider sample from the documentation)

site-packages/django/contrib/admin/utils.py in lookup_field:
289. value = attr(obj)
attr | <function metadata at 0x7f6bf1e7c938>
model_admin | <silver.admin.ProviderAdmin object at 0x7f6bf1e36890>
name | <function metadata at 0x7f6bf1e7c938>
obj | <Provider: Presslabs (Presslabs SRL)>
opts |

site-packages/silver/admin.py in metadata
68. for key, value in obj.meta.items():
d | u''
obj | <Provider: Presslabs (Presslabs SRL)>

I have no idea what's going on other than the object looks like it is a string and not a dictionary, so it doesn't have an items() method.

Thanks for your input.
jérôme

@bogdanpetrea
Copy link
Contributor

I think it's because meta fields are silently expected to be dicts, but they are actually using JSONField, so they can also contain lists, strings, numbers and what else is valid JSON.

In this situation, this could be handled by checking the type of the content of the meta field.
Checking for the list type and displaying the list elements with <code>%s</code>, similary to the already implemented dict case, should be in order. Any other type can remain unformated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants