Comments (11)
Okay, if the field is initially empty and is unedited, I will add some checks to prevent adding empty items.
but if a user clicks on add item and doesn't add anything the issue still happens, which is not really the expected behavior.
You can't really know if the user intends to keep an empty item or not. So, if the user manually adds an empty item, that should be kept in the data.
A new option for dicts to only be stored if any of the values is not null (nested objects are a problem)
I'll provide some validation hooks for this. Before saving the data, your custom validation functions will be called where you can remove non-empty dict keys.
I will try and include this in the next release (maybe next week).
from django-jsonform.
You can't really know if the user intends to keep an empty item or not. So, if the user manually adds an empty item, that should be kept in the data.
That's fair, it's a weird case were both storing as empty or not storing could be interpreted as valid.
I will try and include this in the next release (maybe next week).
Thank you so much.
from django-jsonform.
@alexgmin Hi, I'm working on this right now. I'm looking for some suggestions. If the array is unedited, should I save an empty array ([ ]
) in the db or keep the field completely empty (null
)?
Edit: I think saving an empty array makes more sense because then it saves us from dealing with db errors if json field is not nullable.
from django-jsonform.
I think []
makes more sense too, but what if the field has a default? Shouldn't it be that?
from django-jsonform.
Ah, thanks for reminding me that.
from django-jsonform.
Hi, I've taken care of the arrays. They will be empty initially if min items is 0
or undefined.
Regarding dicts, I think you can do the data processing in the Model.save()
method. Example:
def process_json(data):
# ... check dict keys here ...
return new_data
class MyModel:
def save(self):
new_data = process_json(self.data_field)
self.data_field = new_data
...
What do you think of this?
from django-jsonform.
So the only change you're making is making it empty in min items isn't set, not setting it to the default if it's empty?
What you suggest is feasible, although I'm not a fan of overriding the save method for this type of things, since the save method isn't called in bulk updates. What I've been currently doing for this type of custom things, like #18 is creating a custom field and overriding the get_prep_value or the pre_save methods
class ActionTimestampsJSONFieldWithSchema(JSONFieldWithSchema):
# Remove values that are empty strings
def pre_save(self, model_instance, add):
value = super().pre_save(model_instance, add
for timestamp_key in value.copy().keys():
if value[timestamp_key] == '':
del value[timestamp_key]
return value
I also have a similar case in which I want an object to be null if none of boolean keys inside are true, because that makes querying that field much simpler.
If the min items issue is resolved I can work with the rest.
from django-jsonform.
So the only change you're making is making it empty in min items isn't set, not setting it to the default if it's empty?
Yes, if a default is provided it will use that value instead of being blank.
I'll also add a pre_save_hook
parameter for your use case. If you have a better suggestion, do let me know.
I'll try to release the new version by tomorrow.
from django-jsonform.
A pre_save_hook
would allow me to remove all of of my overrided fields, so I would be extremely grateful.
from django-jsonform.
I've released v2.10.0 which fixes this.
Please upgrade and test out the new version.
from django-jsonform.
@bhch Perfect, will do. I also sent you an email about a private issue, could you check it?
from django-jsonform.
Related Issues (20)
- Default layout of jsonform in Django Admin HOT 4
- Bug when using multi select with custom title HOT 5
- Object of type UUID is not JSON serializable HOT 1
- New release HOT 1
- Custom validation of object properties not showing error next to field HOT 2
- Form dynamics not working after reloading django-jsonform with HTMX HOT 3
- Using an object with empty Properties, having all properties inside AdditionalProperties HOT 3
- Setting JSONField schema dynamically HOT 3
- Copy-pasting / Duplications for Array items and Objects
- Horizontal display of properties HOT 1
- Dynamic header generation for array items HOT 2
- Customising "Add key" and "Add item" texts HOT 1
- Add button to open link in URLField HOT 5
- Decimal values not allowed when entering integer minimum and maximum for 'number' types HOT 4
- Define defaults for an array HOT 3
- Subfields in ArrayField should get the relevant input type
- Issue with errors for fields inside an anyOf schema not displaying correctly HOT 1
- validators for jsonfield are not updated with dynamic schema HOT 3
- Cooperation with other repository `django-pydantic-field` for model schema validation HOT 1
- Title is not populated for `oneOf` when referenced through `$ref` HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-jsonform.