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

Ability to define custom computed properties on models #844

Closed
1 task
lekhnath opened this issue Mar 10, 2023 · 2 comments
Closed
1 task

Ability to define custom computed properties on models #844

lekhnath opened this issue Mar 10, 2023 · 2 comments
Labels
enhancement A feature that exists, works as intended but needs to be improved feature request A feature that does not yet exist but will be a good addition to the library

Comments

@lekhnath
Copy link

lekhnath commented Mar 10, 2023

Describe the feature as you'd like to see it
Define computed property on Models

What do we currently have to do now?

class Product(Model):
    
    @property
     def amount_total(self):
        return self.rate * self.qty

Additional context
This is just an example but it would be nice if we could define custom properties on model and also select these
property dynamically using the select method. I've tried using __cast_map__ I could not manage to get the
desired result. It is not selectable and the value returned from get method of Caster class also did not reflect in result.

Demo implementation of __cast_map__:

from masoniteorm.models import Model

class UsernameCaster:
    def get(self, value):
        return value.split('@')[0]

    def set(self, value):
        pass

class User(Model):
    __selects__ = ['id', 'email as username']
    __cast_map__ = {
        "username": UsernameCaster,
    }
  • Is this a breaking change? Not Sure
@lekhnath lekhnath added enhancement A feature that exists, works as intended but needs to be improved feature request A feature that does not yet exist but will be a good addition to the library labels Mar 10, 2023
@josephmancuso
Copy link
Member

i think you're referring to mutators and accessors. if not ill reopen

https://orm.masoniteproject.com/models#accessors-and-mutators-getter-and-setter

@josephmancuso
Copy link
Member

if you want to seect this you would have to use the add_select clause for the query builder. if you want the logic in the model you could use a scope and add it there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature that exists, works as intended but needs to be improved feature request A feature that does not yet exist but will be a good addition to the library
Projects
None yet
Development

No branches or pull requests

2 participants