save方法将实例保存到两个不同的表,AgencyBeforeReg和Agency,我有一个问题,保存服务字段是manyToMany字段,怎么办呢?
def save(self, force_insert=False, force_update=False, using=None):
if self.id and self.active:
self.sign = 'f'
if not self.active:
self.sign = 'c'
self.deactivate_date = datetime.datetime.now()
super(AgencyBeforeReg, self).save(force_insert, force_update, using)
agency = Agency.objects.create(
id=self.id,
unpf=self.unpf,
unp=self.unp,
egr=self.egr,
title=self.title,
reg_date=self.reg_date,
reg_organ=self.reg_organ,
post_index=self.post_index,
place=self.place,
office=self.office,
# services=self.services,
ins_date=self.ins_date,
edit_date=self.edit_date
)
agency.services.add()
答案 0 :(得分:0)
前几天我遇到了同样的问题。多对多字段与其他字段不同。您需要在保存后设置服务,代码将如下所示
agency = Agency.objects.create(
id=self.id,
unpf=self.unpf,
unp=self.unp,
egr=self.egr,
title=self.title,
reg_date=self.reg_date,
reg_organ=self.reg_organ,
post_index=self.post_index,
place=self.place,
office=self.office,
ins_date=self.ins_date,
edit_date=self.edit_date
)
agency.save()
for service in self.services:
agency.services.add(service)
就我而言,它的工作正常。