在 Django 中设置 ManyToMany 字段默认值的正确方法
The right way to set the default value of a ManyToMany field in django
设置多对多字段默认值的正确方法是什么?这是我在下面尝试的方法,但没有用。我知道我可以覆盖 save 方法,但是每次更新模型时不会调用它吗?我只想在每次创建实例时设置模型的初始值。
def default_values():
return [c.id for c in SomeOtherModel.objects.filter(otherfield__isnull = True)]
class SomeModel(models.Model):
somefield = models.ManyToManyField('SomeField', default=default_values)
semeotherfield = models.ForeignKey('SomeOtherField')
我正在使用 django 1.8
您可以覆盖保存方法,并在其中插入检查主键是否为空。如果它是空的 - 这是对象的创建。
您还可以连接到 post 保存信号 - 有属性告诉您这是否是对象的创建。
问题中提到的方法不起作用,因为ManyToManyField
在保存时不能有值(值必须用RelatedManager
添加)。因此 ManyToManyField
的 default
选项在保存对象时不使用(当调用 <object>.save()
或其他时):即使它被设置(例如返回对象列表的可调用对象) , 保存时该字段将设置为空列表。
根据this ticket:
default
option value is used in Django Form set up to edit this
object. In particular, in Django Admin, the multivalue drop-down will
include for the field, that will contain all the objects returned by
the callable defined as the default value - and they will be all
selected. If the user saves the object without any edit (by pushing
the form's "save" button), then the field will indeed be set to all
the values.
您可以覆盖模型的 init 方法,但这不允许您设置 m2m。
您可以覆盖模型的初始化,例如:
def __init__(self, *args, **kwargs):
if 'initial' not in kwargs:
kwargs['initial'] = {}
kwargs['initial'].update(product_template_admin_initial_values())
super(ProductTemplateAdminForm, self).__init__(*args, **kwargs)
但是那将不允许使用该请求。
如果您依赖于请求,您可以使用管理员 get_changeform_initial_data
def get_changeform_initial_data(self, request):
return {'name': 'custom_initial_value'}
您可以使用请求作为参数和 return 字典来调用方法。 m2m 默认值应该是一个列表
设置多对多字段默认值的正确方法是什么?这是我在下面尝试的方法,但没有用。我知道我可以覆盖 save 方法,但是每次更新模型时不会调用它吗?我只想在每次创建实例时设置模型的初始值。
def default_values():
return [c.id for c in SomeOtherModel.objects.filter(otherfield__isnull = True)]
class SomeModel(models.Model):
somefield = models.ManyToManyField('SomeField', default=default_values)
semeotherfield = models.ForeignKey('SomeOtherField')
我正在使用 django 1.8
您可以覆盖保存方法,并在其中插入检查主键是否为空。如果它是空的 - 这是对象的创建。
您还可以连接到 post 保存信号 - 有属性告诉您这是否是对象的创建。
问题中提到的方法不起作用,因为ManyToManyField
在保存时不能有值(值必须用RelatedManager
添加)。因此 ManyToManyField
的 default
选项在保存对象时不使用(当调用 <object>.save()
或其他时):即使它被设置(例如返回对象列表的可调用对象) , 保存时该字段将设置为空列表。
根据this ticket:
default
option value is used in Django Form set up to edit this object. In particular, in Django Admin, the multivalue drop-down will include for the field, that will contain all the objects returned by the callable defined as the default value - and they will be all selected. If the user saves the object without any edit (by pushing the form's "save" button), then the field will indeed be set to all the values.
您可以覆盖模型的 init 方法,但这不允许您设置 m2m。
您可以覆盖模型的初始化,例如:
def __init__(self, *args, **kwargs):
if 'initial' not in kwargs:
kwargs['initial'] = {}
kwargs['initial'].update(product_template_admin_initial_values())
super(ProductTemplateAdminForm, self).__init__(*args, **kwargs)
但是那将不允许使用该请求。
如果您依赖于请求,您可以使用管理员 get_changeform_initial_data
def get_changeform_initial_data(self, request):
return {'name': 'custom_initial_value'}
您可以使用请求作为参数和 return 字典来调用方法。 m2m 默认值应该是一个列表