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

Can't use Model class as field value #2185

Closed
mikemill opened this issue May 19, 2020 · 3 comments
Closed

Can't use Model class as field value #2185

mikemill opened this issue May 19, 2020 · 3 comments

Comments

@mikemill
Copy link

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.

@coleifer
Copy link
Owner

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.

@coleifer coleifer reopened this May 19, 2020
@coleifer
Copy link
Owner

Thanks for reporting, this is fixed in master now.

@mikemill
Copy link
Author

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.

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

2 participants