Jaymon / prom

A PostgreSQL or SQLite orm for Python
MIT License
22 stars 4 forks source link

ModelData.instance_orm method #170

Closed Jaymon closed 6 months ago

Jaymon commented 1 year ago

I recently had a situation where I wanted to add some additional fields to get_<ORM-NAME>_fields and have those set on the created orm instance, there is currently no way to do this, so I'm proposing:

def get_foo_fields(self, **kwargs):
    kwargs["extra_field"] = "some value"
    return self.get_orm_fields(**kwargs)

def instance_foo(self, instance, **kwargs):
    instance.extra_field = kwargs["extra_field"]
    return instance

I'd have to make sure the lifecycle of the **kwargs continues so that the extra_field can make it all the way to instance_orm.

Another approach without the created_orm method, would be to have some value you can set in get_orm_fields and those will be automatically added to the instance. This might be the better approach, there can still be a method that can be overridden but it would be nice to handle everything in the get_<NAME>_fields method.

I could also name the method: get_orm_instance which would fit better with get_orm_fields