stefanofontanelli/ColanderAlchemy

add vs edit schema

tisdall opened this issue · 5 comments

Related to #74, would people be interested in adding the ability to generate a schema for "add" vs "edit"?

For example, if you generated an "add" schema for a primary key column that had an auto-incrementing integer, you'd want missing=colander.drop to allow the DB to fill it in on insert. However, if you were editing an entry, you couldn't leave it blank because it was an edit and further more you'd probably want it to be a read-only type of widget so the edit applied to the right row in the table.

I came across this issue too. I was thinking there must be some sort of heuristics to guess which fields are autopopulated, which cannot be added upon creation and then filter the rest out. For now I am going to do it with explicit includes.

@miohtama - Yes, I have a heuristic I'm using already, but haven't added it into ColanderAlchemy. I mentioned the major one above... making the primary key(s) an uneditable field.

I've got something related: I'm have a sequence of objects that I want to load into an edit form. What's the best way to use dictify() to get something that Deform will be happy with?

I have the following schema:

class DateSchema(SequenceSchema):

    date = SQLAlchemySchemaNode(Date)


class AllDatesSchema(Schema):

    dates = DateSchema()

And a query that returns all relevant date objects (they have other attributes. I'm currently creating a SQLAlchemySchemaNode(Date) on the fly and calling dictify() on it but I'm sure there must be a better way. Any ideas?

When it comes to handling the submitted form in this case I can only think of deleting and re-adding which is fine in my use case.

@Themanwithoutaplan - I think you posted this in the wrong place... This issue is about having an add/edit toggle for generating the schema.

As for your issue, I'm a little confused about what you're doing... dictify() is for when you've generated a schema from the SQLA object and you want to create an appstruct based on content from an SQLA object. So, you should start from an SQLA class, call SQLAlchemySchemaNode() on that class to generate a schema, then pass a loaded object to the dictify() method to create an appstruct.

Thanks for the explanation. Re. dictify() I'm just looking for the best way to hook up a sequence of objects in an edit form: mapping (from SQLA) in a sequence in a schema. At the moment I loop through the query that returns all my objects (there is no object that represents all the sequence), manually instantiate an SQLAlchemySchemaNode of the objects and append the dict of the instance in the appstruct. This works okay but feels a bit clunky. Is there a better way of doing this?