我有一个django模型,我想根据实例所在的环境(用户登录的环境)附加一条额外的信息。出于这个原因,我不想在数据库级别进行
这样可以吗?还是有我没有预见到的问题
在models.py中
class FooOrBar(models.Model):
"""Type is 'foo' or 'bar'
"""
def __init__(self, type):
self.type = type
在views.py中
class FooCheck(FooOrBar):
"""Never saved to the database
"""
def __init__(self, foo_or_bar):
self.__dict__ = foo_or_bar.__dict__.copy()
def check_type(self, external_type):
if external_type == 'foo':
self.is_foo = True
else:
self.is_foo = False
foos_or_bars = FooOrBar.objects.all()
foochecks = map(FooCheck, foos_or_bars)
for foocheck in foochecks:
foocheck.check_type('foo')
额外的信用问题:有没有一种更有效的方法来调用多个对象上的一个方法,即用聪明的东西替换最后一个forloop
好吧,这样不行。试图删除
FooOrBar
对象会引发对为了避免这个问题,我不打算继承FooOrBar的遗产,但如果有人对更好的方法有什么建议,我会很感兴趣的
我也有类似的问题,我做了一些类似的事情:
然后你可以使用
其中
instance
是FooBar实例相关问题 更多 >
编程相关推荐