derek-schaefer / django-json-field Goto Github PK
View Code? Open in Web Editor NEWGeneric JSON model and form fields.
License: BSD 3-Clause "New" or "Revised" License
Generic JSON model and form fields.
License: BSD 3-Clause "New" or "Revised" License
Not present in: commit 462edd22e5577ff9ee6477f7572b571700d1a2aa
I noticed this issue starting with: commit b787ef2dbff957c5b5a64be13f01f985d17ff05b
It might of been introduced in between those 2 commits. Please note that this 2 commits are for Django and not django-json-field.
Error bellow:
Internal Server Error: /phpanel/library/47/
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/core/handlers/base.py", line 113, in get_response
response = wrapped_callback(request, _callback_args, *_callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/contrib/admin/options.py", line 550, in wrapper
return self.admin_site.admin_view(view)(_args, *_kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/utils/decorators.py", line 105, in _wrapped_view
response = view_func(request, _args, *_kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/views/decorators/cache.py", line 52, in _wrapped_view_func
response = view_func(request, _args, *_kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/contrib/admin/sites.py", line 202, in inner
return view(request, _args, *_kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/contrib/admin/options.py", line 1420, in change_view
return self.changeform_view(request, object_id, form_url, extra_context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/utils/decorators.py", line 29, in _wrapper
return bound_func(_args, *_kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/utils/decorators.py", line 105, in _wrapped_view
response = view_func(request, _args, *_kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/utils/decorators.py", line 25, in bound_func
return func.get(self, type(self))(_args2, *_kwargs2)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/db/transaction.py", line 362, in inner
return func(_args, _kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/contrib/admin/options.py", line 1356, in changeform_view
ModelForm = self.get_form(request, obj)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/contrib/admin/options.py", line 609, in get_form
fields = flatten_fieldsets(self.get_fieldsets(request, obj))
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/contrib/admin/options.py", line 347, in get_fieldsets
return [(None, {'fields': self.get_fields(request, obj)})]
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/contrib/admin/options.py", line 598, in get_fields
form = self.get_form(request, obj, fields=None)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/contrib/admin/options.py", line 634, in get_form
return modelform_factory(self.model, **defaults)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/forms/models.py", line 541, in modelform_factory
return type(form)(class_name, (form,), form_class_attrs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/forms/models.py", line 284, in new
opts.help_texts, opts.error_messages)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/forms/models.py", line 214, in fields_for_model
formfield = formfield_callback(f, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/contrib/admin/options.py", line 211, in formfield_for_dbfield
return db_field.formfield(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/json_field/fields.py", line 197, in formfield
return super(JSONField, self).formfield(**defaults)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/db/models/fields/init.py", line 1859, in formfield
return super(TextField, self).formfield(**defaults)
File "/usr/local/lib/python2.7/dist-packages/Django-1.7a2-py2.7.egg/django/db/models/fields/init.py", line 836, in formfield
return form_class(_defaults)
File "/usr/local/lib/python2.7/dist-packages/json_field/forms.py", line 17, in init
super(JSONFormField, self).init(_args, **kwargs)
TypeError: init() got an unexpected keyword argument 'max_length'
Hi!
I've been using a JSONField implementation I found floating around, but it's not as good as this project.
The one bug is, though, when I use django.forms.models.model_to_dict.
I'm confused about value_for_object here, because it returns the json representation,
while surely it could return the decoded json object?
My suggested fix would be to remove the method; the default superclass functionality seems good.
Or what do you think?
Hello,
i was comparing your django-json-field with django-jsonfield and the one included in django-extensions, and your is the best for my needs (write a real generic value... not a dict or list of generic values).
but running you test cases, i noticed that test_decimal() FAIL, reporting:
self.assertEqual(Decimal(1.24), self.json_model.objects.get(pk=t1.pk).json)
AssertionError: Decimal('1.2399999999999999911182158029987476766109466552734375') != u'1.2399999999999999911182158029987476766109466552734375'
what we can do for that?
Package will not work in Django 1.9 due to deprecated import in forms
:
`django_json_field-0.5.7-py2.7.egg/json_field/forms.py:5: RemovedInDjango19Warning: The django.forms.util module has been renamed. Use django.forms.utils instead.``
There is no necessary to import django.forms.util(s)
, because you're using it to raise ValidationError. Just import forms
and change util.ValidationError
to forms.ValidationError
, or import ValidationError
from django.core.exceptions
.
for install in the django , the name of package is jsonfield , not json_field
I haven't managed to install the package.
#settings.py
INSTALLED_APPS = (
...
'json_field',
...
)
#models.py
from json_field import JSONField
from django.db import models
...
class Human(models.Model):
phone_numbers = JSONField()
and when doing south migration:
File "<...>/models.py", line 2, in <module>
from json_field import JSONField
ImportError: cannot import name JSONField
I tried Django shell
(test)Dae-MacPro:test Dae$ python manage.py shell
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from json_field import JSONField
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: cannot import name JSONField
I'm using Django 1.4. I tried installing from repo and pip, no difference.
What might be the problem?
Thanks
I was thinking about adding validation to the form field using jsonschema before saving it to the database. Would this be useful because I'm doing it manually right now and the exception is logged but I would like to return the error to the user as to what happend and how to fix it?
I've noticed that django-json-field treats my floating point data as Decimal type. I've seen why you did it by looking at the issues history of the project on GitHub, but I think it's really inconvenient, here's why:
Could you make an option to be used in the field declaration to treat floats as floats? E.g.
f = JSONField(null=True, pure_float=True)
... or something like it?
Hi, I had this error in Django 1.3 with djangotoolbox and AppEngine because inside JSONField.db_type you are not passing the 'connection' param as a keyword argument, here's a fragment of the stack-trace:
File "/.../django/db/models/base.py", line 462, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/.../django/db/models/base.py", line 573, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File ".../django/db/models/manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)
File "/.../django/db/models/query.py", line 1438, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/.../dbindexer/compiler.py", line 38, in execute_sql
return super(SQLInsertCompiler, self).execute_sql(return_id=return_id)
File ".../db/basecompiler.py", line 368, in execute_sql
db_type = field.db_type(connection=self.connection)
File "/.../django/db/models/fields/subclassing.py", line 28, in inner
return func(*args, **kwargs)
File "/.../json_field/fields.py", line 102, in db_type
return super(JSONField, self).db_type(connection)
File "/.../django/db/models/fields/subclassing.py", line 28, in inner
return func(*args, **kwargs)
File "/.../django/db/models/fields/subclassing.py", line 28, in inner
return func(*args, **kwargs)
for some reason, inside subclassing.py, django 1.3 and djangotoolbox's indexer is messing with the connection
param when calling the following function:
def call_with_connection(func):
arg_names, varargs, varkwargs, defaults = getargspec(func)
updated = ('connection' in arg_names or varkwargs)
if not updated:
warn("A Field class whose %s method hasn't been updated to take a "
"`connection` argument." % func.__name__,
DeprecationWarning, stacklevel=3)
def inner(*args, **kwargs):
if 'connection' not in kwargs:
from django.db import connection
kwargs['connection'] = connection
warn("%s has been called without providing a connection argument. " %
func.__name__, DeprecationWarning,
stacklevel=2)
if updated:
return func(*args, **kwargs)
if 'connection' in kwargs:
del kwargs['connection']
return func(*args, **kwargs)
return inner
I fixed it using this diff but I haven't tested it with other versions
That feature, while interesting, isn't necessary for most applications.
It's also, by my admittedly inexact measurement, over three times slower:
In [25]: %timeit json.loads(s)
10000 loops, best of 3: 30.8 us per loop
In [26]: %timeit json.loads(s, cls = JSONDecoder)
10000 loops, best of 3: 100 us per loop
line 6 of fields.py imports django.utils.timezone, which isn't introduced until Django 1.4, whereas setup.py claims to only require 1.2.7
Is there a reason why you don't return the json.loads
?
e.g. forms.DateField.clean
returns a date
and forms.BooleanField.clean
returns a bool
.
how YOU doin?
I create a new field on my model as such:
some_json_field = JSONField(default=[], blank=True)
However, when I generate the schemamigration (using python manage.py schemamigration appname --auto) and migrate, the field on existing model instances for the some_json_field are the string "[]"
instead of []
Steps to reproduce
>>> import json_field, dateutil
>>> json_field.__version__
'0.5.4'
>>> dateutil.__version__
'2.2'
>>> from json_field.fields import JSONDecoder
>>> from django.utils import simplejson as json
>>>
>>> json_payload = '{"title": "2014-01-27 | Title with date"}'
>>>
>>> json.loads(json_payload) # default decoder - OK
{u'title': u'2014-01-27 | Title with date'}
>>>
>>> json.loads(json_payload, cls=JSONDecoder)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 351, in loads
return cls(encoding=encoding, **kw).decode(s)
File "/Users/caxap/Work/pd/api/venv/lib/python2.7/site-packages/json_field/fields.py", line 76, in decode
obj[key] = self.decode(value, recurse=True)
File "/Users/caxap/Work/pd/api/venv/lib/python2.7/site-packages/json_field/fields.py", line 85, in decode
return date_parser.parse(obj).date()
File "/Users/caxap/Work/pd/api/venv/lib/python2.7/site-packages/dateutil/parser.py", line 748, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/Users/caxap/Work/pd/api/venv/lib/python2.7/site-packages/dateutil/parser.py", line 310, in parse
res, skipped_tokens = self._parse(timestr, **kwargs)
TypeError: 'NoneType' object is not iterable
I'm having troubles with django beta's because you use simplejson that is already deprecated and deleted in dev's versions of django. Please, switch to Python json.
When installing from a empty virtualenv, I get this:
Obtaining django-json-field from git+git://github.com/chronossc/django-json-field.git#egg=django-json-field (from -r requirements.txt (line 12))
Updating /home/crmadm/virtualenvs/crm-prd/src/django-json-field clone
Running setup.py egg_info for package django-json-field
Traceback (most recent call last):
File "", line 14, in
File "/home/crmadm/virtualenvs/crm-prd/src/django-json-field/setup.py", line 1, in
import json_field
File "json_field/init.py", line 1, in
from django.core.exceptions import ImproperlyConfigured
ImportError: No module named django.core.exceptions
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "", line 14, in
File "/home/crmadm/virtualenvs/crm-prd/src/django-json-field/setup.py", line 1, in
import json_field
File "json_field/init.py", line 1, in
from django.core.exceptions import ImproperlyConfigured
ImportError: No module named django.core.exceptions
After installs Django, it works.
Currently it shows something like this:
<django.utils.functional.proxy object at 0x1072622d0> (Caught "No JSON object could be decoded")
Could be a nicer message i think.
Think hiding __builtins__
helps? Nope.
(x for x in (1).__class__.__base__.__subclasses__() if x.__name__ == 'catch_warnings').next()()._module.__builtins__
For whatever reason, the encoder_kwargs and decoder_kwags kwargs are required.
https://github.com/derek-schaefer/django-json-field/blob/master/json_field/fields.py#L162
default
from model's field has higher priority than form field's initial
property.
django-json-field is incompatible with Django 1.5 due to the switch from simplejson to json. This results in the following error:
TypeError: __init__() got an unexpected keyword argument 'namedtuple_as_object'
I'm not yet familiar enough with django-json-field to know what the right fix is but I'm looking into it. Would love some help from someone with a better understanding.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.