How can I make a Django model read-only?

后端 未结 3 1697
后悔当初
后悔当初 2021-02-01 19:47

Is it possible to make a Django model read only? No creating, updating etc.

N.B. this question is different to:

Make a Django model read-only? (this question all

3条回答
  •  自闭症患者
    2021-02-01 20:34

    You can set managed = False in your model's Meta:

    from django.db import models
    
    class MyReadOnlyModel(models.Model):
        class Meta:
            managed = False
    

    If False, no database table creation or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means.

    https://docs.djangoproject.com/en/dev/ref/models/options/#managed

提交回复
热议问题