Custom JSONResponse response for FastAPI – cannot access field in subclass

I want my FastAPI application to return JSON:API responses. It would be really cumbersome to craft each response manually so I’ve looked into creating a custom response class, one based on JSONResponse, since the responses have to be in JSON format.

They have an example here:

https://fastapi.tiangolo.com/advanced/custom-response/#custom-response-class

So, subclass your response class (in my case JSONResponse), subclass it, implement the render method.

So far so good:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class JSONAPIResponse(JSONResponse):
def render(self, content: Any) -> Any:
# Null object.
if content is None:
[...]
</code>
<code>class JSONAPIResponse(JSONResponse): def render(self, content: Any) -> Any: # Null object. if content is None: [...] </code>
class JSONAPIResponse(JSONResponse):

    def render(self, content: Any) -> Any:

        # Null object.
        if content is None:
    [...]

However, the problem comes at the next step.[1]

If I add a content_object_type to JSONAPIResponse, I put it in the __init__ method and everything, FastAPI still complains about it.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
class JSONAPIResponse(JSONResponse):
content_object_type: str
def __init__(self, content: Any, content_object_type: str):
super().__init__(content=content)
self.content_object_type = content_object_type
def render(self, content: Any) -> Any:
self.content_object_type
[... Elsewhere in the code base, another method ...]
return JSONAPIResponse(content=jsonable_encoder(content_object), content_object_type="bla")
</code>
<code> class JSONAPIResponse(JSONResponse): content_object_type: str def __init__(self, content: Any, content_object_type: str): super().__init__(content=content) self.content_object_type = content_object_type def render(self, content: Any) -> Any: self.content_object_type [... Elsewhere in the code base, another method ...] return JSONAPIResponse(content=jsonable_encoder(content_object), content_object_type="bla") </code>

class JSONAPIResponse(JSONResponse):

    content_object_type: str

    def __init__(self, content: Any, content_object_type: str):
        super().__init__(content=content)

        self.content_object_type = content_object_type

    def render(self, content: Any) -> Any:

        self.content_object_type

    [... Elsewhere in the code base, another method ...]

    return JSONAPIResponse(content=jsonable_encoder(content_object), content_object_type="bla")

Boom ???? this is the FastAPI error message below

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def render(self, content: Any) -> Any:
self.content_object_type
</code>
<code>def render(self, content: Any) -> Any: self.content_object_type </code>
def render(self, content: Any) -> Any:

  self.content_object_type

E AttributeError: ‘JSONAPIResponse’ object has no attribute ‘content_object_type’

So I’ve declared the attribute, I have it in the constructor that’s used… what’s wrong?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật