-
Notifications
You must be signed in to change notification settings - Fork 153
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
New feature: allow nested attributes (#70) #71
Conversation
It was required to change the codebase for I did it with a simple also, I had issues using |
I'm certainly may have missed a simpler solution and would love to use it...do you have a working example using the For me, that works fine for reading existing values, but does not allow posting/patching new values. It looked to me like that was because of the way the new object is created in alchemy.py--if the schema declares the field as a Relationship than it is treated specially and the related schema object is created, but otherwise it tries to build the object directly by just unpacking the arguments. |
Indeed nested or list would not create related objects, they would simply set the value into the model, to create related objects, in jsonapi is creating the object on another endpoint and then link then on a relationship endpoint |
Once again, the point here is not to created a related object, it is to allow complex attributes. The JSONAPI spec is clear that this should be allowed, but currently this library doesn't seem to allow it. From the JSONAPI spec: http://jsonapi.org/format/#document-resource-object-attributes
The intent of this PR is to allow the use of the Nested fields type to easily allow complex attributes. Security attributes/tags are an example of when one might want to have such complex attributes: for example when we want to manage a list of key/value pairs that define access rules, but that should not themselves ever be addressed as independent objects. |
I'm +1 on this PR. Otoh, if there's currently a good way, how to create one "parent" object with multiple new related objects in one POST, it would be great. Is it possible? If that so, it should be documented somewhere, or at least small example here would be extremely useful. |
I took some time to test this:
a workaround is to create a setter property, which can create your related models, let the model handle the inline attribute |
@hellupline : thanks for flagging...I had not directly supported non-list-like elements originally, but I have updated the PR to support that case as well (you're right, it should, per the spec). |
@jcampbell , atually, the object I used was a list, but the field was a JSON field, your patch is enforcing to use a DB relationships for nested data ( in your example, Person_Tag model ) you should use |
It would help me if you could describe what you're expecting or concerned about...it seems correct to me to expect the relationship in the model schema, since that is what is implied by a Marshmallow Nested field. If you'd like a different approach, could you elaborate a bit more? If you simply used a JSON field type, you would not be using nested at all (and further, that would be making a strong assumption about the DB backing behind SQLAlchemy since that isn't part of SQL directly). |
@jcampbell , my concern is:
Example: class Address(Schema):
zip_code = String(required=True)
country = String(missing='Brazil')
state = String(required=True)
city = String(required=True)
district = String(required=True)
street = String(required=True)
number = String(required=True)
complement = String(missing='')
class Person(Schema):
address_list = Nested(Address, many=True)
class PersonModel(ModelBase):
address_list = Column(JSON(), default='[]', nullable=False, server_default='[]') in this example, there is no need "address_list" to be a second table, its a nested data in the PersonModel table my concern is that your patch could interpret the input of this schema as sqlalchemy relationship. I do agreed that be able to create nested relationships is nice ( I even have a perfect case for this ) |
@hellupline I've updated the patch to use inspection as you suggested, which should address the case you identified. It's a bit tricky to fully test with the current unit testing suite since JSON isn't a standard SQL type. I've tested it locally using postgres backing to ensure that both serialization/deserialization and marshmallow schema validation work. |
@jcampbell , indeed SQLAlchemy is yet to add JSON column type to sqlite |
@hellupline, any chance we can get this PR merged? It seems that a .07% decrease in coverage for adding 1 lines seems acceptable when it is making the library more compliant with the spec. |
@sodre I am not the owner of the repo, just a annoying dev with strict code quality rules ( I do code review ). @akira-dev what do you think, its acceptable ? |
@akira-dev, ping 👍 |
@akira-dev, any change you can take a look at this PR? It is about 1-year old now. |
Just sorry I will merge this pr today or tomorrow thanks for your work |
Thanks a lot for your work and sorry for the delay |
Modifies create_object and update_object to allow attributes defined by fields.List and fields.Nested, with new tests and example.