Is it possible to make a Django model read only? No creating, updating etc.
是否可以将Django模型设为只读?没有创建,更新等
N.B. this question is different to:
注:这个问题不同于:
Make a Django model read-only? (this question allows creation of new records)
将Django模型设为只读? (这个问题允许创建新记录)
Whole model as read-only (only concerns the Django admin interface - I'd like the model to be read only throughout the whole app)
整个模型为只读(仅涉及Django管理界面 - 我希望模型只能在整个应用程序中读取)
3 个解决方案
#1
16
Override the save and delete methods for the model. How are you planning to add objects to your model?
覆盖模型的保存和删除方法。您打算如何向模型添加对象?
def save(self, *args, **kwargs):
return
def delete(self, *args, **kwargs):
return
#2
2
To be absolutely sure that your model is read-only, you can use the DATABASE_ROUTERS setting to disable writing on a per model basis:
为了确保您的模型是只读的,您可以使用DATABASE_ROUTERS设置禁用每个模型的写入:
# settings.py
DATABASE_ROUTERS = ('dbrouters.MyCustomRouter', )
# dbrouters.py
class MyCustomRouter(object):
def db_for_write(self, model, **hints):
if model == MyReadOnlyModel:
raise Exception("This model is read only. Shame!")
return None
I would consider this an insurance policy, and not the primary way to solve the problem. Mikael's answer, for example, is great but doesn't cover all cases because some Django operations bypass delete
and save
methods.
我认为这是一个保险单,而不是解决问题的主要方法。例如,Mikael的答案很棒但并未涵盖所有情况,因为一些Django操作绕过了删除和保存方法。
See Juan José Brown's answer in Django - how to specify a database for a model? for a more detailed description of using a database router.
请参阅JuanJoséBrown在Django中的答案 - 如何为模型指定数据库?有关使用数据库路由器的更详细说明。
#3
-1
Througth this is very old question - it will be usefull: https://docs.djangoproject.com/en/dev/ref/models/options/#managed
通过这是一个非常古老的问题 - 它将是有用的:https://docs.djangoproject.com/en/dev/ref/models/options/#managed
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.
如果为False,则不会对此>>>模型执行数据库表创建或删除操作。如果模型表示已通过其他方式创建>>>的现有表或数据库视图,则此选项非常有用。
#1
16
Override the save and delete methods for the model. How are you planning to add objects to your model?
覆盖模型的保存和删除方法。您打算如何向模型添加对象?
def save(self, *args, **kwargs):
return
def delete(self, *args, **kwargs):
return
#2
2
To be absolutely sure that your model is read-only, you can use the DATABASE_ROUTERS setting to disable writing on a per model basis:
为了确保您的模型是只读的,您可以使用DATABASE_ROUTERS设置禁用每个模型的写入:
# settings.py
DATABASE_ROUTERS = ('dbrouters.MyCustomRouter', )
# dbrouters.py
class MyCustomRouter(object):
def db_for_write(self, model, **hints):
if model == MyReadOnlyModel:
raise Exception("This model is read only. Shame!")
return None
I would consider this an insurance policy, and not the primary way to solve the problem. Mikael's answer, for example, is great but doesn't cover all cases because some Django operations bypass delete
and save
methods.
我认为这是一个保险单,而不是解决问题的主要方法。例如,Mikael的答案很棒但并未涵盖所有情况,因为一些Django操作绕过了删除和保存方法。
See Juan José Brown's answer in Django - how to specify a database for a model? for a more detailed description of using a database router.
请参阅JuanJoséBrown在Django中的答案 - 如何为模型指定数据库?有关使用数据库路由器的更详细说明。
#3
-1
Througth this is very old question - it will be usefull: https://docs.djangoproject.com/en/dev/ref/models/options/#managed
通过这是一个非常古老的问题 - 它将是有用的:https://docs.djangoproject.com/en/dev/ref/models/options/#managed
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.
如果为False,则不会对此>>>模型执行数据库表创建或删除操作。如果模型表示已通过其他方式创建>>>的现有表或数据库视图,则此选项非常有用。