Skip to content

Commit 05bc5b1

Browse files
committed
Add obj parameter for upload method
1 parent e290c88 commit 05bc5b1

22 files changed

Lines changed: 197 additions & 19 deletions

File tree

docs/build.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ def read_cls_docstring(cls):
4444

4545
def get_versions():
4646
return [
47+
{
48+
"version": "0.4.9",
49+
"changes": ["Add obj parameter to upload_file method."],
50+
},
4751
{
4852
"version": "0.4.8",
4953
"changes": [
@@ -426,7 +430,6 @@ def read_example(rel_path: str) -> str:
426430

427431

428432
def get_page_context(page_url):
429-
430433
from fastadmin import InlineModelAdmin, ModelAdmin, WidgetType, widget_action
431434
from fastadmin.models.base import BaseModelAdmin
432435
from fastadmin.models.schemas import (
@@ -759,7 +762,7 @@ async def save_model(self, id, payload):
759762
},
760763
{
761764
"type": "text",
762-
"content": "For file and image fields use <code>UploadFile</code> and <code>UploadImage</code> widgets in <code>formfield_overrides</code>. Implement <code>upload_file(obj, field_name, file_name, file_content)</code> on the model admin to handle uploads; it must return the file URL (e.g. after saving to disk or S3).",
765+
"content": "For file and image fields use <code>UploadFile</code> and <code>UploadImage</code> widgets in <code>formfield_overrides</code>. Implement <code>upload_file(field_name, file_name, file_content, obj=None)</code> on the model admin to handle uploads; it must return the file URL (e.g. after saving to disk or S3).",
763766
},
764767
{
765768
"type": "text",

docs/index.html

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ <h4 class="title">FastAdmin</h4>
192192

193193
<ul class="nav flex-column">
194194

195+
<li class="nav-item">
196+
<a class="nav-link" href="#v0_4_9">v0.4.9</a>
197+
</li>
198+
195199
<li class="nav-item">
196200
<a class="nav-link" href="#v0_4_8">v0.4.8</a>
197201
</li>
@@ -383,7 +387,7 @@ <h1>FastAdmin | Documentation</h1>
383387
<div class="row">
384388
<div class="col-sm-6 col-lg-4">
385389
<ul class="list-unstyled">
386-
<li><strong>Version:</strong> 0.4.8</li>
390+
<li><strong>Version:</strong> 0.4.9</li>
387391
<li>
388392
<strong>Author:</strong>
389393
<a href="mailto:[email protected]" target="_blank">
@@ -400,7 +404,7 @@ <h1>FastAdmin | Documentation</h1>
400404
</li>
401405
<li>
402406
<strong>Updated:</strong>
403-
27 March 2026
407+
13 April 2026
404408
</li>
405409
</ul>
406410
</div>
@@ -917,6 +921,7 @@ <h3>Registering Models</h3>
917921
from fastapi.middleware.cors import CORSMiddleware
918922
from models import BaseEvent, Event, Tournament, User, UserAttachment
919923
from tortoise.contrib.fastapi import RegisterTortoise
924+
from tortoise.models import Model
920925

921926
from fastadmin import (
922927
TortoiseInlineModelAdmin,
@@ -956,12 +961,14 @@ <h3>Registering Models</h3>
956961
field_name: str,
957962
file_name: str,
958963
file_content: bytes,
964+
obj: Model | None = None,
959965
) -> str:
960966
"""This method is used to upload files.
961967

962968
:params field_name: a name of field.
963969
:params file_name: a name of file.
964970
:params file_content: a content of file.
971+
:params obj: the existing ORM model instance when uploading on the change page, or None on the add page.
965972
:return: A file url.
966973
"""
967974
# save file to media directory or to s3/filestorage here
@@ -1017,7 +1024,16 @@ <h3>Registering Models</h3>
10171024
field_name: str,
10181025
file_name: str,
10191026
file_content: bytes,
1027+
obj: Model | None = None,
10201028
) -> None:
1029+
"""This method is used to upload files.
1030+
1031+
:params field_name: a name of field.
1032+
:params file_name: a name of file.
1033+
:params file_content: a content of file.
1034+
:params obj: the existing ORM model instance when uploading on the change page, or None on the add page.
1035+
:return: A file url.
1036+
"""
10211037
# save file to media directory or to s3/filestorage here
10221038
return f"/media/{file_name}"
10231039

@@ -1601,17 +1617,20 @@ <h3>Registering Models</h3>
16011617
user.password = password
16021618
user.save()
16031619

1604-
async def upload_file(
1620+
def upload_file(
16051621
self,
16061622
field_name: str,
16071623
file_name: str,
16081624
file_content: bytes,
1625+
obj: models.Model | None = None,
16091626
) -> str:
16101627
"""This method is used to upload files.
16111628

16121629
:params field_name: a name of field.
16131630
:params file_name: a name of file.
16141631
:params file_content: a content of file.
1632+
:params obj: the existing ORM model instance when uploading on the change page,
1633+
or None when uploading on the add (create) page.
16151634
:return: A file url.
16161635
"""
16171636
# save file to media directory or to s3/filestorage here
@@ -2052,12 +2071,14 @@ <h3>Registering Models</h3>
20522071
field_name: str,
20532072
file_name: str,
20542073
file_content: bytes,
2074+
obj: Base | None = None,
20552075
) -> str:
20562076
"""This method is used to upload files.
20572077

20582078
:params field_name: a name of field.
20592079
:params file_name: a name of file.
20602080
:params file_content: a content of file.
2081+
:params obj: the existing ORM model instance when uploading on the change page, or None when on the add page.
20612082
:return: A file url.
20622083
"""
20632084
# save file to media directory or to s3/filestorage here
@@ -2539,17 +2560,19 @@ <h3>Registering Models</h3>
25392560
obj.password = password
25402561
commit()
25412562

2542-
async def upload_file(
2563+
def upload_file(
25432564
self,
25442565
field_name: str,
25452566
file_name: str,
25462567
file_content: bytes,
2568+
obj: db.Entity | None = None,
25472569
) -> str:
25482570
"""This method is used to upload files.
25492571

25502572
:params field_name: a name of field.
25512573
:params file_name: a name of file.
25522574
:params file_content: a content of file.
2575+
:params obj: an orm/db model object. None on the add page, the existing instance on the change page.
25532576
:return: A file url.
25542577
"""
25552578
# save file to media directory or to s3/filestorage here
@@ -3370,6 +3393,7 @@ <h3>Methods and Attributes</h3>
33703393
# Avoid scientific notation for Decimal values in API responses,
33713394
# e.g. 3.75E+3 -> "3750"
33723395
value = format(value, "f")
3396+
33733397
serialized_dict[field.name] = value
33743398
if inspect.iscoroutinefunction(obj.__str__):
33753399
str_fn = obj.__str__
@@ -3446,6 +3470,7 @@ <h3>Methods and Attributes</h3>
34463470
return datetime.datetime.fromisoformat(value).date()
34473471
case WidgetType.DateTimePicker:
34483472
return datetime.datetime.fromisoformat(value)
3473+
34493474
case _:
34503475
return value
34513476

@@ -3528,12 +3553,14 @@ <h3>Methods and Attributes</h3>
35283553
field_name: str,
35293554
file_name: str,
35303555
file_content: bytes,
3556+
obj: Model | None = None,
35313557
) -> str:
35323558
"""This method is used to upload files.
35333559

35343560
:params field_name: a name of field.
35353561
:params file_name: a name of file.
35363562
:params file_content: a content of file.
3563+
:params obj: an orm/db model object. None on the add (create) page; the existing model instance on the change (edit) page.
35373564
:return: A file url.
35383565
"""
35393566
raise NotImplementedError
@@ -3874,7 +3901,7 @@ <h3>Form Field Types</h3>
38743901

38753902

38763903
<p class="text-4">
3877-
For file and image fields use <code>UploadFile</code> and <code>UploadImage</code> widgets in <code>formfield_overrides</code>. Implement <code>upload_file(obj, field_name, file_name, file_content)</code> on the model admin to handle uploads; it must return the file URL (e.g. after saving to disk or S3).
3904+
For file and image fields use <code>UploadFile</code> and <code>UploadImage</code> widgets in <code>formfield_overrides</code>. Implement <code>upload_file(field_name, file_name, file_content, obj=None)</code> on the model admin to handle uploads; it must return the file URL (e.g. after saving to disk or S3).
38783905
</p>
38793906

38803907

@@ -4018,6 +4045,7 @@ <h3>Registering Inlines</h3>
40184045
from fastapi.middleware.cors import CORSMiddleware
40194046
from models import BaseEvent, Event, Tournament, User, UserAttachment
40204047
from tortoise.contrib.fastapi import RegisterTortoise
4048+
from tortoise.models import Model
40214049

40224050
from fastadmin import (
40234051
TortoiseInlineModelAdmin,
@@ -4057,12 +4085,14 @@ <h3>Registering Inlines</h3>
40574085
field_name: str,
40584086
file_name: str,
40594087
file_content: bytes,
4088+
obj: Model | None = None,
40604089
) -> str:
40614090
"""This method is used to upload files.
40624091

40634092
:params field_name: a name of field.
40644093
:params file_name: a name of file.
40654094
:params file_content: a content of file.
4095+
:params obj: the existing ORM model instance when uploading on the change page, or None on the add page.
40664096
:return: A file url.
40674097
"""
40684098
# save file to media directory or to s3/filestorage here
@@ -4118,7 +4148,16 @@ <h3>Registering Inlines</h3>
41184148
field_name: str,
41194149
file_name: str,
41204150
file_content: bytes,
4151+
obj: Model | None = None,
41214152
) -> None:
4153+
"""This method is used to upload files.
4154+
4155+
:params field_name: a name of field.
4156+
:params file_name: a name of file.
4157+
:params file_content: a content of file.
4158+
:params obj: the existing ORM model instance when uploading on the change page, or None on the add page.
4159+
:return: A file url.
4160+
"""
41224161
# save file to media directory or to s3/filestorage here
41234162
return f"/media/{file_name}"
41244163

@@ -4702,17 +4741,20 @@ <h3>Registering Inlines</h3>
47024741
user.password = password
47034742
user.save()
47044743

4705-
async def upload_file(
4744+
def upload_file(
47064745
self,
47074746
field_name: str,
47084747
file_name: str,
47094748
file_content: bytes,
4749+
obj: models.Model | None = None,
47104750
) -> str:
47114751
"""This method is used to upload files.
47124752

47134753
:params field_name: a name of field.
47144754
:params file_name: a name of file.
47154755
:params file_content: a content of file.
4756+
:params obj: the existing ORM model instance when uploading on the change page,
4757+
or None when uploading on the add (create) page.
47164758
:return: A file url.
47174759
"""
47184760
# save file to media directory or to s3/filestorage here
@@ -5153,12 +5195,14 @@ <h3>Registering Inlines</h3>
51535195
field_name: str,
51545196
file_name: str,
51555197
file_content: bytes,
5198+
obj: Base | None = None,
51565199
) -> str:
51575200
"""This method is used to upload files.
51585201

51595202
:params field_name: a name of field.
51605203
:params file_name: a name of file.
51615204
:params file_content: a content of file.
5205+
:params obj: the existing ORM model instance when uploading on the change page, or None when on the add page.
51625206
:return: A file url.
51635207
"""
51645208
# save file to media directory or to s3/filestorage here
@@ -5640,17 +5684,19 @@ <h3>Registering Inlines</h3>
56405684
obj.password = password
56415685
commit()
56425686

5643-
async def upload_file(
5687+
def upload_file(
56445688
self,
56455689
field_name: str,
56465690
file_name: str,
56475691
file_content: bytes,
5692+
obj: db.Entity | None = None,
56485693
) -> str:
56495694
"""This method is used to upload files.
56505695

56515696
:params field_name: a name of field.
56525697
:params file_name: a name of file.
56535698
:params file_content: a content of file.
5699+
:params obj: an orm/db model object. None on the add page, the existing instance on the change page.
56545700
:return: A file url.
56555701
"""
56565702
# save file to media directory or to s3/filestorage here
@@ -6597,6 +6643,31 @@ <h2>Changelog</h2>
65976643

65986644

65996645

6646+
<section id="v0_4_9">
6647+
<h3>v0.4.9</h3>
6648+
6649+
6650+
6651+
<p class="text-4">
6652+
Add obj parameter to upload_file method.
6653+
</p>
6654+
6655+
6656+
6657+
6658+
6659+
6660+
6661+
6662+
6663+
6664+
6665+
6666+
6667+
6668+
</section>
6669+
6670+
66006671
<section id="v0_4_8">
66016672
<h3>v0.4.8</h3>
66026673

examples/django_djangoorm/orm/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,15 @@ def upload_file(
138138
field_name: str,
139139
file_name: str,
140140
file_content: bytes,
141+
obj: models.Model | None = None,
141142
) -> str:
142143
"""This method is used to upload files.
143144
144145
:params field_name: a name of field.
145146
:params file_name: a name of file.
146147
:params file_content: a content of file.
148+
:params obj: the existing ORM model instance when uploading on the change page,
149+
or None when uploading on the add (create) page.
147150
:return: A file url.
148151
"""
149152
# save file to media directory or to s3/filestorage here

examples/fastapi_ponyorm/example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,14 @@ def upload_file(
9191
field_name: str,
9292
file_name: str,
9393
file_content: bytes,
94+
obj: db.Entity | None = None,
9495
) -> str:
9596
"""This method is used to upload files.
9697
9798
:params field_name: a name of field.
9899
:params file_name: a name of file.
99100
:params file_content: a content of file.
101+
:params obj: an orm/db model object. None on the add page, the existing instance on the change page.
100102
:return: A file url.
101103
"""
102104
# save file to media directory or to s3/filestorage here

examples/fastapi_sqlalchemy/example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,14 @@ async def upload_file(
9191
field_name: str,
9292
file_name: str,
9393
file_content: bytes,
94+
obj: Base | None = None,
9495
) -> str:
9596
"""This method is used to upload files.
9697
9798
:params field_name: a name of field.
9899
:params file_name: a name of file.
99100
:params file_content: a content of file.
101+
:params obj: the existing ORM model instance when uploading on the change page, or None when on the add page.
100102
:return: A file url.
101103
"""
102104
# save file to media directory or to s3/filestorage here

0 commit comments

Comments
 (0)