我遇到了类似的问题。目标是有一个类,允许某些字段承担组的值。下面的代码将这些功能引入您的模型:
- 如果要让字段继承组值,只需创建另一个名为“inherit_group_”的 BooleanField。
- 如果您设置可继承字段的值,“inherit_group_”的值会自动设置为 False(因为您已设置显式值)。
- 如果将“inherit_group_”设置为 True,则关联字段的值将返回到组的值。
- 处理其他所有内容,包括保存和初始化。
一个警告 - 如果您在此模型中使用 ModelForm,则需要覆盖保存功能,以便最后设置所有“inherit_group_ 属性”,因为当设置关联的位置字段时,“inherit_group_”将如上所述设置为 False。该代码也在下面。这整个事情可能最好通过创建新类来处理 - InheritableModel 和 InheritableModelForm。不过我懒得这样做:)。
这是模型的代码:
_inheritable_fields = []
def __init__(self, *args, **kwargs):
super(Location, self).__init__(*args, **kwargs)
# Change iheritable field values to group value if current value is
# None
self._inheritable_fields = [
fname for fname in dir(self)
if hasattr(self, 'inherit_group_%s' % fname)]
# Make sure that all fields are in the correct state given the
# inherit_group values
[setattr(self, 'inherit_group_%s' % fname, getattr(self,
'inherit_group_%s' % fname))
for fname in self._inheritable_fields]
def __setattr__(self, name, val):
super(Location, self).__setattr__(name, val)
if name == "group" and val:
# A new group was specified. Update all fields that are currently
# inheriting from the group
[models.Model.__setattr__(self, fname, getattr(self.group, fname))
for fname in self._inheritable_fields
if getattr(self, 'inherit_group_%s' % fname)]
elif name in self._inheritable_fields:
# An inheritable field value changed. Update its inheritance state
models.Model.__setattr__(self, 'inherit_group_%s' % name, False)
elif name.startswith('inherit_group_'):
field_name = re.sub('^inherit_group_', '', name)
if val and field_name in self._inheritable_fields:
# An inheritance state (e.g., inherit_group_name) was changed.
# Change value back to group value
if hasattr(self, 'group'):
models.Model.__setattr__(self, field_name,
getattr(self.group, field_name))
else:
models.Model.__setattr__(self, field_name, None)
def save(self, *args, **kwargs):
# Set all fields using the inherited value to None for DB storage.
val_from_group = [
fname for fname in self._inheritable_fields
if getattr(self, 'inherit_group_%s' % fname)]
[models.Model.__setattr__(self, fname, None) for fname in val_from_group]
super(Location, self).save(*args, **kwargs)
# Return the fields changed above back to the group values.
[models.Model.__setattr__(self, fname, getattr(self.group, fname))
for fname in self._inheritable_fields
if getattr(self, 'inherit_group_%s' % fname)]
这是 ModelForm 的代码:
def save(self, commit=True):
location = super(LocationForm, self).save(commit=False)
# location.inherit_group_x has to be set last as it'll be set to
# False when it's associated field is set
[setattr(location, 'inherit_group_%s' % fname,
self.cleaned_data['inherit_group_%s' % fname])
for fname in location._inheritable_fields]
if commit:
location = super(LocationForm, self).save()
return location