'pydantic: Using property.getter decorator for a field with an alias

scroll all the way down for a tl;dr, I provide context which I think is important but is not directly relevant to the question asked

A bit of context

I'm in the making of an API for a webapp and some values are computed based on the values of others in a pydantic BaseModel. These are used for user validation, data serialization and definition of database (NoSQL) documents.

Specifically, I have nearly all resources inheriting from a OwnedResource class, which defines, amongst irrelevant other properties like creation/last-update dates:

  • object_key -- The key of the object using a nanoid of length 6 with a custom alphabet
  • owner_key -- This key references the user that owns that object -- a nanoid of length 10.
  • _key -- this one is where I'm bumping into some problems, and I'll explain why.

So arangodb -- the database I'm using -- imposes _key as the name of the property by which resources are identified.

Since, in my webapp, all resources are only accessed by the users who created them, they can be identified in URLs with just the object's key (eg. /subject/{object_key}). However, as _key must be unique, I intend to construct the value of this field using f"{owner_key}/{object_key}", to store the objects of every user in the database and potentially allow for cross-user resource sharing in the future.

The goal is to have the shortest per-user unique identifier, since the owner_key part of the full _key used to actually access and act upon the document stored in the database is always the same: the currently-logged-in user's _key.

My attempt

My thought was then to define the _key field as a @property-decorated function in the class. However, Pydantic does not seem to register those as model fields.

Moreover, the attribute must actually be named key and use an alias (with Field(... alias="_key"), as pydantic treats underscore-prefixed fields as internal and does not expose them.

Here is the definition of OwnedResource:

class OwnedResource(BaseModel):
    """
    Base model for resources owned by users
    """

    object_key: ObjectBareKey = nanoid.generate(ID_CHARSET, OBJECT_KEY_LEN)
    owner_key: UserKey
    updated_at: Optional[datetime] = None
    created_at: datetime = datetime.now()

    @property
    def key(self) -> ObjectKey:
        return objectkey(self.owner_key)

    class Config:
        fields = {"key": "_key"} # [1]

[1] Since Field(..., alias="...") cannot be used, I use this property of the Config subclass (see pydantic's documentation)

However, this does not work, as shown in the following example:

@router.post("/subjects/")
def create_a_subject(subject: InSubject):
    print(subject.dict(by_alias=True))

with InSubject defining properties proper to Subject, and Subject being an empty class inheriting from both InSubject and OwnedResource:

class InSubject(BaseModel):
    name: str
    color: Color
    weight: Union[PositiveFloat, Literal[0]] = 1.0
    goal: Primantissa # This is just a float constrained in a [0, 1] range
    room: str

class Subject(InSubject, OwnedResource):
    pass

When I perform a POST /subjects/, the following is printed in the console:

{'name': 'string', 'color': Color('cyan', rgb=(0, 255, 255)), 'weight': 0, 'goal': 0.0, 'room': 'string'}

As you can see, _key or key are nowhere to be seen.

Please ask for details and clarification, I tried to make this as easy to understand as possible, but I'm not sure if this is clear enough.

tl;dr

A context-less and more generic example without insightful context:

With the following class:

from pydantic import BaseModel

class SomeClass(BaseModel):
    
    spam: str

    @property
    def eggs(self) -> str:
        return self.spam + " bacon"

    class Config:
        fields = {"eggs": "_eggs"}

I would like the following to be true:

a = SomeClass(spam="I like")
d = a.dict(by_alias=True)
d.get("_eggs") == "I like bacon"


Solution 1:[1]

You might be able to serialize your _key field using a pydantic validator with the always option set to True.

Using your example:

from typing import Optional
from pydantic import BaseModel, Field, validator


class SomeClass(BaseModel):

    spam: str
    eggs: Optional[str] = Field(alias="_eggs")

    @validator("eggs", always=True)
    def set_eggs(cls, v, values, **kwargs):
        """Set the eggs field based upon a spam value."""
        return v or values.get("spam") + " bacon"


a = SomeClass(spam="I like")
my_dictionary = a.dict(by_alias=True)
print(my_dictionary)
> {'spam': 'I like', '_eggs': 'I like bacon'}
print(my_dictionary.get("_eggs"))
> "I like bacon"

So to serialize your _eggs field, instead of appending a string, you'd insert your serialization function there and return the output of that.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1