我在我的 django/satchmo 继承的模型产品中完成了一个 pre_save 信号,称为 JPiece,我从 satchmo 类别继承了另一个模型,称为 JewelCategory。pre_save 信号使 JPiece 对象获取类别列表并将那些符合 Jpiece 描述的类别添加到关系中,这是在模型中完成的,这意味着如果我手动执行
p = Jpiece.objects.get(pk=3) p.save()
类别已保存并添加到 p.category m2m 关系中,但如果我从管理员保存,则不会执行此操作...
我怎样才能做到这一点......从管理员那里保存一个 JPiece 并获得它所属的类别......
以下是模型,请记住它们都具有来自 satchmo 产品和类别类的模型继承。
class Pieza(Product):
codacod = models.CharField(_("CODACOD"), max_length=20,
help_text=_("Unique code of the piece. J prefix indicates silver piece, otherwise gold"))
tipocod = models.ForeignKey(Tipo_Pieza, verbose_name=_("Piece Type"),
help_text=_("TIPOCOD"))
tipoenga = models.ForeignKey(Engaste, verbose_name=_("Setting"),
help_text=_("TIPOENGA"))
tipojoya = models.ForeignKey(Estilos, verbose_name=_("Styles"),
help_text=_("TIPOJOYA"))
modelo = models.CharField(_("Model"),max_length=8,
help_text=_("Model No. of casting piece."),
blank=True, null=True)
def autofill(self):
#self.site = Site.objects.get(pk=1)
self.precio = self.unit_price
self.peso_de_piedra = self.stone_weigth
self.cantidades_de_piedra = self.stones_amount
self.for_eda = self.for_eda_pieza
if not self.id:
self.date_added = datetime.date.today()
self.name = str(self.codacod)
self.slug = slugify(self.codacod, instance=self)
cats = []
self.category.clear()
for c in JewelCategory.objects.all():
if not c.parent:
if self.tipocod in c.tipocod_pieza.all():
cats.append(c)
else:
if self.tipocod in c.tipocod_pieza.all() and self.tipojoya in c.estilo.all():
cats.append(c)
self.category.add(*cats)
def pieza_pre_save(sender, **kwargs):
instance = kwargs['instance']
instance.autofill()
# import ipdb;ipdb.set_trace()
pre_save.connect(pieza_pre_save, sender=Pieza)
我知道我有时可能对我需要什么的解释含糊不清,所以请随时提出任何问题,我一定会尽快澄清,因为这是一个迫切需要这个的客户。
一如既往地感谢大家...