DRY添加创建/修改的方式和时间 [英] DRY way to add created/modified by and time
问题描述
有一些类似
- created_by
- created_date
- modified_by
- modified_date
- created_by
- created_date
- modified_by
- modified_date
对于很多表来说,这是一个很常见的模式。
Would be a very common pattern for a lot of tables.
1)您可以在model.py中自动设置创建的日期(但不是其他日期)
1) You can set created date automatically (but not others) in model.py with
created_date = models.DateTimeField(auto_now_add=True, editable=False)
2)您可以在model.py中创建/修改日期(但不是/没有请求上下文)
2) You could do created/modified dates (but not by/user as don't have request context) in model.py with
def save(self):
if self.id:
self.modified_date = datetime.now()
else:
self.created_date = datetime.now()
super(MyModel,self).save()
3)您可以设置创建/修改日期和admin.py - 但这不处理非管理员更新
3) You could set the created/modifed date and by in admin.py - but this doesn't deal with non admin updates
def save_model(self, request, obj, form, change):
if change:
obj.modified_by = request.user
obj.modified_date = datetime.now()
else:
obj.created_by = request.user
obj.created_date = datetime.now()
obj.save()
4)地方将在view.py中,它可以做所有4,但不包括管理员更新。
4) And the final place would be in the view.py which can do all 4, but doesn't cover admin updates.
所以现实地必须有逻辑扩展,至少重复在3& 4(或从两个方面呼吁的模式,将被遗漏)
So realistically have to have logic spread out, at a minimum repeated in 3 & 4 (or a method on the model called from both, which will be missed)
什么是更好的方法? (我一直在使用python / django几天,所以很容易丢失一些明显的东西)
Whats a better way? (I've been working with python/django for a couple of days so could easily be missing something obvious)
- 你能做某事吗@login_required例如@audit_changes
- 您可以访问模型中的请求和当前用户,并集中逻辑吗?
推荐答案
现在,Django可以处理创建/修改日期,因此可以实现如下:
The create/modification dates can be handled by Django now, so they can be implemented like:
class BaseModel(models.Model):
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
通过将这个添加到抽象模型基类中,它可以轻松地添加到应用程序的所有型号。
By adding this to a abstract model base class, it can be easily added to all models of the application.
存储用户更难,因为 request.user
不可用。正如SeanOC所说,这是web请求和模型层之间的分离。或者您一直传递此字段,或者在threadlocal中存储 request.user
。 Django CMS为其权限系统执行此操作。
Storing the user is harder, since the request.user
is not available. As SeanOC mentioned, this is a separation of concerns between the web request, and model layer. Either you pass this field all the time, or store request.user
in a threadlocal. Django CMS does this for their permission system.
class CurrentUserMiddleware(object):
def process_request(self, request):
set_current_user(getattr(request, 'user', None))
而用户跟踪发生在其他地方:
And the user tracking happens elsewhere:
from threading import local
_thread_locals = local()
def set_current_user(user):
_thread_locals.user=user
def get_current_user():
return getattr(_thread_locals, 'user', None)
对于非Web环境(例如管理命令),您必须调用 set_current_user
在脚本开始。
For non-web environments (e.g. management commands), you'd have to call set_current_user
at the start of the script.
这篇关于DRY添加创建/修改的方式和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!