Git Product home page Git Product logo

Comments (11)

bhch avatar bhch commented on May 23, 2024

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.

alexgmin avatar alexgmin commented on May 23, 2024

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.

bhch avatar bhch commented on May 23, 2024

@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.

alexgmin avatar alexgmin commented on May 23, 2024

I think [] makes more sense too, but what if the field has a default? Shouldn't it be that?

from django-jsonform.

bhch avatar bhch commented on May 23, 2024

Ah, thanks for reminding me that.

from django-jsonform.

bhch avatar bhch commented on May 23, 2024

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.

alexgmin avatar alexgmin commented on May 23, 2024

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.

bhch avatar bhch commented on May 23, 2024

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.

alexgmin avatar alexgmin commented on May 23, 2024

A pre_save_hookwould allow me to remove all of of my overrided fields, so I would be extremely grateful.

from django-jsonform.

bhch avatar bhch commented on May 23, 2024

I've released v2.10.0 which fixes this.

Please upgrade and test out the new version.

from django-jsonform.

alexgmin avatar alexgmin commented on May 23, 2024

@bhch Perfect, will do. I also sent you an email about a private issue, could you check it?

from django-jsonform.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.