Jaymon / prom

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

Field(fset=...) didn't work #97

Closed Jaymon closed 4 years ago

Jaymon commented 4 years ago

This didn't work

from prom import Orm, Field

class BaseOrm(Orm):
    @classmethod
    def normalize(cls, val):
        return "{:0>5}".format(val)

    foo = Field(str, True, size=5, fset=BaseOrm.normalize)

It failed with this stack trace:

  ...
  File "/.../site-packages/prom/model.py", line 440, in modify
    setattr(self, field_name, field_val)
  File "/.../site-packages/prom/model.py", line 466, in __setattr__
    super(Orm, self).__setattr__(field_name, field_val)
  File "/.../site-packages/prom/config.py", line 659, in __set__
    val = self.fset(instance, val)
TypeError: normalize() takes exactly 2 arguments (3 given)
Jaymon commented 4 years ago

This was my bad, this would've worked if I did this:

from prom import Orm, Field

class BaseOrm(Orm):
    @classmethod
    def normalize(cls, instance, val):
        return "{:0>5}".format(val)

    foo = Field(str, True, size=5, fset=BaseOrm.normalize)

Notice the method signature of the normalize method is (cls, instance, val) instead of just (cls, val), I forgot the instance param, that's what I was doing wrong.