Skip to content
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

Field depends #174

Open
Jaymon opened this issue Feb 28, 2024 · 1 comment
Open

Field depends #174

Jaymon opened this issue Feb 28, 2024 · 1 comment

Comments

@Jaymon
Copy link
Owner

Jaymon commented Feb 28, 2024

Ok, the idea would be you could do something like this:

one = Field(str)
two = Field(str)

three = Field(str, depends=["one", "two"])

That will cause Schema.fields to order the fields so three comes after one and two. Likewise, Field.default and Field.fset would be changed to check depends and call a new Field.dget(orm, default_value) method to get the value.

This isn't perfect and I think there might some code paths I'm missing.

What prompted this was email addresses, I wanted to set a hash value when the email address was set, so I modified the address's fset method to set the hash value. The problem was when creating a new Orm instance, Orm.__init__ calls Field.fdefault which would set the fields passed into __init__ to fields["hash"] = None and then it would get passed to Orm.modify which called address's fset method before seeing the fields["hash"] value, which correctly set the value in hash, but then as Orm.modify continued to iterate through fields it eventually got to the hash key and value and then hash got set to None, wiping out the value the address field had previously set hash to.

@Jaymon
Copy link
Owner Author

Jaymon commented Feb 28, 2024

This was a comment in a models file I've been working on:

# TODO @address_hash.setter("address") meaning that it will call
# address_hash.fset(orm, "<VALUE RETURNED FROM ADDRESS FSET>"), it's
# basically a way to say address_hash depends on the value of address. Or we
# could have a .dset method that stands for "depends fset"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant