Skip to content

Commit

Permalink
Ensure only one object can be created chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
shabda committed Feb 9, 2018
1 parent 464cca6 commit e204de5
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/only_one.rst
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`.

0 comments on commit e204de5

Please sign in to comment.