django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

[BUG] Field serialized when using repr=False

Open robinsandstrom opened this issue 1 year ago • 1 comments
trafficstars

Fields are still included when using repr=False. Look at the following example:

Expected behaviour is to have fields with repr=False to not serialize, or am I missing something? :)

from pydantic import BaseModel, Field
from ninja import Schema

class ModelExample(BaseModel):
    height: int
    width: int
    area: int = Field(repr=False)
    ### or  
 
    @computed_field(repr=False)
    @cached_property
    def area(self) -> int:
        return  height*width

class OutSchema(ModelExample, Schema):
     pass

Versions (please complete the following information):

  • Python version: 3.12
  • Django version: 5.01
  • Django-Ninja version: 1.2.x
  • Pydantic version: 2.x

robinsandstrom avatar Aug 19 '24 19:08 robinsandstrom

@robinsandstrom

repr is used for representation (in console) - see docs

basically that means that when you do this:

>>> x = ModelExample(...)
>>>  x
<ModelExample height=1 width=2>
# ^ no area in repr

what you probably need is exclude - see docs

vitalik avatar Aug 20 '24 15:08 vitalik