-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
18 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
How to make a field editable while creating, but read only in existing objects? | ||
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
||
YOu need to make the :code:`name` and :code:`category` read only once a :code:`Hero` is created. However duing the first write the fields needs to be editable. | ||
|
||
You can do this by overriding :code:`get_readonly_fields` method, like this:: | ||
|
||
def get_readonly_fields(self, request, obj=None): | ||
if obj: | ||
return ["name", "category"] | ||
else: | ||
return [] | ||
|
||
:code:`obj` is None during the object creation, but set to the object being edited during an edit. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters