How to replace a Django model field with a property
来源:互联网 发布:all in one seo pack 编辑:程序博客网 时间:2024/06/05 11:31
If you use Django for your web apps, you will probably have come across a situation where you need to run some custom code on assigning a value to a model field. You’ve probably hacked around this by overriding the save() method or some other arcane hackery, but this is not only unnecessary, but it’s more complicated than it should be.
You can use a Python property to add a getter/setter to the field and run your custom code there. Even better, you don’t even have to change your database or run any migrations to do this.
Since you can’t just create a getter and a setter with the same name as the field (Django will be confused, as it will only see the method and not the shadowed field), you can rename the field. My preferred solution is to prepend the field with an underscore, but still keep the same field column name, to keep everything working as it used to.
So if, for example, you have a date field that you need to run some additional logic on, you can do the following:
If the original model looks like this:
class MyClass(models.Model): my_date = models.DateField()
You can add a property like so:
class MyClass(models.Model): _my_date = models.DateField(db_column="my_date") @property def my_date(self): return self._my_date @my_date.setter def my_date(self, value): if value > datetime.date.today(): logger.warning("The date chosen was in the future.") self._my_date = value
This way, the entire codebase can stay as-is, without needing to be updated at all. You can even remove the original field and replace it with other functionality, faking it entirely, or remove the properties in the future when you don’t need the custom behavior. It’s perfectly backwards- and forwards-compatible, and very easy to do.
Subscribe to my mailing list
Did you like what you just read and want to be notified when I post more? Subscribe to my mailing list to get updates on my posts and other random goodies.
- How to replace a Django model field with a property
- XNA How To: Draw a Model with a Custom Effect
- How to POST a django form with AJAX & jQuery
- How to study in a field
- TensorFlow: How to freeze a model and serve it with a python API
- How to Replace a Device in a ZFS Storage Pool
- How to replace a character for a newline in Vim?
- Adding a New Field to the Movie Model and Table
- how to begin with a new project
- How to Do Research With a Professor
- How to build a product with InstallShield
- How To Design a Class With Matlab
- How to set default value for a field
- How to get the field descriptions of a table?
- How to achieve success in a particular field
- How to get the field descriptions of a table
- How to create an EMF model from a Java application ?
- Tensorflow: How to restore a previously saved model (python)
- 【C#】解决devexpress新建的Form窗体没有继承默认样式的问题
- laravel框架的启动过程分析
- java的IO流以及复制粘贴操作
- PHP call_user_func_array()函数
- 从程序员的眼中去看《黑客帝国》
- How to replace a Django model field with a property
- C#实现Base64的编码与解码
- 安卓listview两个类型条目的复用,总是报数组越界Android listview ArrayIndexOutOfBoundsException问题
- ubuntu远程连接windows系统
- Block Change Tracking(块跟踪)详解
- Android 控件在最底部展示
- Mysql limit 优化,百万至千万级快速分页,--复合索引的引用并应用于轻量级框架
- Android-AsyncTask简单介绍和使用
- DB2中,先四舍五入在保存4位小数点的方法