Peewee: Can't use Model class as field value

Created on 19 May 2020  路  3Comments  路  Source: coleifer/peewee

I have a custom field that stores the import path to a Python class. The db_value takes the class object and coverts it to the import path string and the python_value takes the string and imports the object giving me back the class. However, as of peewee 3.13.2 this no longer works and instead gives me peewee.InternalError: (1054, "Unknown column 't1' in 'field list'") when I try to save.

Field:

class PythonObjectField(CharField):
    """
    A custom class for storing a Python object as a reference.
    """

    def db_value(self, value):
        if value is None:
            return None
        elif isinstance(value, str):
            return value

        return get_object_path(value)

    def python_value(self, value):
        if value is None:
            return None

        return import_object(value)

The usage would be Model.field = SomeOtherModel

Full example script

When debugging this I found that this affects my Model derived classes but not a simple one that only extends object. Additionally, I found that db_value is never called.

Most helpful comment

Thank you! I just wanted to drop a note saying how much I've enjoyed using peewee and how appreciative I've been have your responsiveness to issues.

All 3 comments

It isn't calling db_value() because peewee sees that you're using a model class as a value, which is a recognized AST type. The issue that originated this fix is #2131.

Thanks for reporting, this is fixed in master now.

Thank you! I just wanted to drop a note saying how much I've enjoyed using peewee and how appreciative I've been have your responsiveness to issues.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vkrizan picture vkrizan  路  3Comments

GMaxera picture GMaxera  路  4Comments

ghost picture ghost  路  5Comments

rayzorben picture rayzorben  路  4Comments

mmongeon-aa picture mmongeon-aa  路  4Comments