Skip to content

About image avatars #604

Answered by jowilf
ps96068 asked this question in Q&A
Nov 8, 2024 · 1 comments · 3 replies
Discussion options

You must be logged in to vote

You can create your custom ImageField and override the serialize_value method. You only need to return a dictionary with filename, content-type, and url.

@dataclass
class MyImageField(BaseImageField):
     async def serialize_value(
        self, request: Request, value: Any, action: RequestAction
    ) -> dict:
              return {"url": request.url_for...}

then using it your view

class MyModelView(ModelView):
       fields = [... , MyImageField("avatar"), ... ]
       

Example: https://github.com/jowilf/starlette-admin/blob/main/starlette_admin/contrib/sqla/fields.py#L57

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@ps96068
Comment options

@jowilf
Comment options

@ps96068
Comment options

Answer selected by jowilf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants