Limit number of model instances to be created - django

前端 未结 6 1524
萌比男神i
萌比男神i 2020-12-02 20:36

I have model from which I only want to create one instance, and no more instances should be allowed.

Is this possible? I\'ve got a feeling that I\'ve seen this done

6条回答
  •  伪装坚强ぢ
    2020-12-02 21:21

    If you only want one instance of a model , perhaps 'there is an app for that!'

    You can check django-solo witch is a complete solution for what you need.

    here is a link -> https://github.com/lazybird/django-solo

    It comes with a class for singleton models and one for admin singleton models, witch does not add an s to the end of the model name, exclude the intermediary screen witch lists all objects and also removes the 'add' button and the 'save and add another' button.

    It also comes with some other fluffy and useful stuff like importing the singletons directly on your templates, etc...

提交回复
热议问题