You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I commonly need to make sure the input is valid in relation to other fields, there are a couple places you can currently do this, using modify_fields or to_interface. Here are some ideas to make this more robust:
assure_fields
Here is an example of a modify_fields that sets values based on other values:
defmodify_fields(self, fields):
ret= {}
foo=fields.pop("foo", None)
bar=fields.pop("bar", None)
iffooandbar:
ifnotfields.get("foo_hash", None):
fields["foo_hash"] =self.hash_value(String(foo), bar)
ifnotfields.get("bar_hash", None):
fields["bar_hash"] =self.hash_value(bar, String(foo))
else:
iffooandnotfields.get("foo_hash", None):
raiseValueError("You cannot set foo without bar")
ifbarandnotfields.get("bar_hash", None):
raiseValueError("You cannot set bar without foo")
iffields.get("che", None):
che=fields.pop("che")
ifcheandnotfields.get("che_hash", None):
fields["che_hash"] =self.hash_che(che)
# find all the passed in fields that should be in the misc dictmisc_fields=fields.pop("misc", self.misc)
forkinself.schema.fields.keys():
ifkinfields:
ret[k] =fields.pop(k)
misc_fields.update(fields)
ret["misc"] =misc_fieldsreturnret
As you can see, foo is dependent on bar, and things like that. It works to do it here but this is only ran when all fields are modified, so this wouldn't get run when something is set individually, something like this:
fb=FooBar()
fb.foo="blah"
So I'm wondering if I can add an assure_fields method that would get called in to_interface and modify and would take a fields and allow you to normalize the fields and shift everything around, basically exactly what modify_fields is for but is ran more often to make sure the Orm instance is in the correct state.
Bad things is it would be run multiple times in a row if you did something like FooBar.create(**kwargs) because it would get run in modify and then ran again in to_interface but I don't know if that is awful for the assurance that it does give that the data is what is expected.
Individual Field methods
I could also add an assure method on the Field that would be called with a signature like this:
defassure(self, orm, fields)
and then it would return the correct value from the fields dict. You could even segment this more and have iassure() for going into the db and fassure for setting the field's value.
The text was updated successfully, but these errors were encountered:
I commonly need to make sure the input is valid in relation to other fields, there are a couple places you can currently do this, using
modify_fields
orto_interface
. Here are some ideas to make this more robust:assure_fields
Here is an example of a
modify_fields
that sets values based on other values:As you can see,
foo
is dependent onbar
, and things like that. It works to do it here but this is only ran when all fields are modified, so this wouldn't get run when something is set individually, something like this:So I'm wondering if I can add an
assure_fields
method that would get called into_interface
andmodify
and would take afields
and allow you to normalize the fields and shift everything around, basically exactly whatmodify_fields
is for but is ran more often to make sure the Orm instance is in the correct state.Bad things is it would be run multiple times in a row if you did something like
FooBar.create(**kwargs)
because it would get run inmodify
and then ran again into_interface
but I don't know if that is awful for the assurance that it does give that the data is what is expected.Individual Field methods
I could also add an
assure
method on the Field that would be called with a signature like this:and then it would return the correct value from the
fields
dict. You could even segment this more and haveiassure()
for going into the db andfassure
for setting the field's value.The text was updated successfully, but these errors were encountered: