-
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.
Ensure only one object can be created chapter
- Loading branch information
Showing
1 changed file
with
14 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,16 @@ | ||
How to allow creating only one object from the admin? | ||
===================================================== | ||
|
||
The UMSRA admin has asked you to limit the number of categories to only one. They want every entity to be of the same category. | ||
|
||
You can do this by:: | ||
|
||
MAX_OBJECTS = 1 | ||
|
||
def has_add_permission(self, request): | ||
if self.model.objects.count() >= MAX_OBJECTS: | ||
return False | ||
return super().has_add_permission(request) | ||
|
||
This would hide the add buton as soon as one object is created. You can set :code:`MAX_OBJECTS` to any value to ensure that | ||
larger number of objects can't be ceated than :code:`MAX_OBJECTS`. |