abbasovalex / django-splitjsonwidget-form Goto Github PK
View Code? Open in Web Editor NEWDjango form's widget that renders field JSON data as group fields that can edited.
Django form's widget that renders field JSON data as group fields that can edited.
Thank you for this awesome library.
Solved many issues, but having this problem:
original data: {1: {'end_minute': 0, 'start_hour': 10, 'end_hour': 19, 'start_minute': 0}, 2: {'end_minute': 30, 'start_hour': 8, 'end_hour': 17, 'start_minute': 30}}
After saving it using django-admin it's getting saved as below in DB.
[{u'end_minute': u'0', u'start_hour': u'10', u'end_hour': u'19', u'start_minute': u'0'}, {u'end_hour': u'17', u'start_hour': u'8', u'end_minute': u'30', u'start_minute': u'30'}]
I found that the fields are merged each other when it used multiple time in the model.
It results in storing the same contents with each other.
For objects which have digital key after form saved they will be transformed to a list.
Hi There,
Jsonsplit does not accept floats. Shoud just suffice to add float in the test against unicode and int in _to_build. Will do a pull request in this.
Cheers
Helge
Hi,
Thank you for sharing a useful widget.
I have some unicode contents in json (backslashreplaced).
Whenever I'm about to use the widget on those contents, it crashes.
Since I've already resolved this issue in my project, I could send a pull-request sooner or later.
Will add common cases:
There had been this backward-incompatible change in Django 2.1:
https://docs.djangoproject.com/en/2.1/releases/2.1/#features-removed-in-2-1.
Need to fix the method signature of render(), so that it looks like this:
def render(self, name, value, attrs=None, renderer=None):
Problem:
You cannot use the list which includes similar structure objects.
example:
json = {
...
[
{'name': 'A', 'value': 'No'},
{'name': 'B', 'value': 'No'},
{'name': 'C', 'value': 'Yes'}
],
...
}
after save it will look approximately as:
json = {
...
[
{'name': 'A', 'value': 'No'},
{'name': 'C', 'value': 'Yes},
],
...
}
it happened because a values' keys of the list were rewrote.
Will discover this code and make solution:
74 if k.find(self.separator) is not -1:
~ 75 apx, _, nk = k.rpartition(self.separator)
76 try:
+ 77 # parse list
78 int(nk)
~ 79 l = []
+ 80 obj = {}
+ 81 if apx != root_node:
+ 82 for key, val in copy_raw_data.items():
+ 83 _, _, t = key.rpartition(self.separator)
+ 84 if key is k:
+ 85 del copy_raw_data[key]
+ 86 elif key.startswith(apx):
+ 87 try:
+ 88 int(t)
+ 89 l.append(val)
+ 90 except ValueError:
+ 91 obj[t] = val <---------- may be here?
+ 92 del copy_raw_data[key]
+ 93 if obj:
+ 94 l.append(obj)
+ 95 l.append(v)
+ 96 return _to_parse_key(apx, l)
97 except ValueError:
When upgrading to django 1.11 an error arises: build_attrs() unexpected keyword 'type'.
It happened in other django related software, as you can see:
django-ckeditor/django-ckeditor#375
jazzband/django-sortedm2m#107
It is easily solved by this recoding in widgets.py:
--- /usr/local/lib/python2.7/dist-packages/splitjson/widgets.py.orig 2018-03-07 13:16:58.544426000 +0100
+++ /usr/local/lib/python2.7/dist-packages/splitjson/widgets.py 2018-03-07 13:24:15.904531209 +0100
@@ -23,8 +23,9 @@
Widget.__init__(self, attrs)
def _as_text_field(self, name, key, value, is_sub=False):
- attrs = self.build_attrs(self.attrs, type='text',
- name="%s%s%s" % (name, self.separator, key))
+ attrs = self.build_attrs(self.attrs)
+ attrs['type'] = 'text'
+ attrs['name'] = "%s%s%s" % (name, self.separator, key)
attrs['value'] = utils.encoding.force_unicode(value)
attrs['id'] = attrs.get('name', None)
return u""" <label for="%s">%s:</label>
It would be great if you can update your code to solve the problem. Thanks!
How can I use it for this model for example?
forms.py
class PersonForm(forms.ModelForm):
name = forms.CharField( max_length=100)
extra_field = forms.CharField(widget=SplitJSONWidget(attrs=attrs, debug=True))
class Meta:
model = Person
and in views.py
def record_person(request):
if request.method == "POST":
form = PersonForm(request.POST)
if form.is_valid():
person_instance = form.save()
return redirect('homepage')
else:
thisperson = Person.objects.get(user=request.user)
form = PersonForm(instance=thisperson)
return render(request, "record_person.html", {'form': form})
the extra_field
store json data, which are custom additional field inputs that the user can create.
the extra_field
is displayed, but I've got, even if thisperson
does exist:
Source data:
None
I am using Django 1.8 version, don't know whether it's a version related problem. But after adding all code in respective forms.py, views.py, and test_template.py, It returns
testForm' object has no attribute 'get'
I think i might missed to add the appname in settings.py apps.
i tried the default name but it didn't worked , it's not pointed in you repo readme too.
Here is the output,
Request Method: GET
Request URL: http://framework.com/test_template/
Django Version: 1.8.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'framework')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
response = middleware_method(request, response)
if response.get('X-Frame-Options', None) is not None:
Exception Type: AttributeError at /test_template/
Exception Value: 'testForm' object has no attribute 'get'
I had reached here using this ,
http://stackoverflow.com/a/16650451/4190386
and found it as the best way to implement my json form.
Could you please look into it?
I was trying to run this library on python 3, and this line threw a syntax error. It's a simple fix to add brackets to the print statement, and looking through the code, there don't seem to be any other issues that stop python 3 support.
Can we use it with admin? seems like, it will increase the readability compared to displaying the value in textfield.
How can we get the updated JSON after submitting the form.
if float(get_version()) < 1.9:
from django.forms.util import flatatt
else:
from django.forms.utils import flatatt
ValueError: invalid literal for float(): 1.6.11
Thanks
render() got an unexpected keyword argument 'renderer'
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.