0

I have done a pre_save signal in my django/satchmo inherited model Product called JPiece and I have another model inheritance from satchmo Category called JewelCategory. The pre_save signal makes the JPiece objects get the category list and add those categories that fit the Jpiece description to the relation, that is done in the model, meaning if I manually do

p = Jpiece.objects.get(pk=3) p.save()

The categories are saved and added to the p.category m2m relation but If i save from the admin it does not do this...

How can I achieve this... to save from the admin a JPiece and to get the categories it belongs too...

Here are the models remember that they both have model inheritance from satchmo product and category classes.

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)

I know I can be vague with explanations sometimes of what I need so please feel free to ask anything Ill be sure to clarify ASAP since this is a client that needs this urgently.

Thank you all as always...

maumercado
  • 1,453
  • 4
  • 23
  • 47

1 Answers1

1

If you use pre_save, it's called before save(), meaning you can't define m2m relationships since the model doesn't have an ID.

Use post_save.

# this works because the ID does exist
p = Jpiece.objects.get(pk=3) 
p.save()

Update, check out the comment here: Django - How to save m2m data via post_save signal?

It looks like the culprit now is that with an admin form, there is a save_m2m() happening AFTER the post_save signal, which could be overwriting your data. Can you exclude the field from the form in your ModelAdmin?

# django.forms.models.py
if commit:
    # If we are committing, save the instance and the m2m data immediately.
    instance.save()
    save_m2m()
Community
  • 1
  • 1
Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
  • I just changed the signal to post_save and still when I click on save in the admin no categories ar added or deleted from the JPiece object. – maumercado Mar 17 '11 at 22:39
  • Oh, I suppose it should throw an exception. Which means... is that autofill() code even running? is `JPiece` the same as your `Piezo` object you show in the code btw? – Yuji 'Tomita' Tomita Mar 17 '11 at 22:46
  • Umm yes sorry for that the Pieza objects IS JPiece... sorry for that... and the def autofill method is inside the class Pieza – maumercado Mar 17 '11 at 22:50
  • Can you try excluding the category field from your admin form? I'm starting to think that the field is being overridden by whatever you submitted in your admin (blank?) – Yuji 'Tomita' Tomita Mar 17 '11 at 22:59
  • Yes it was being overriden, by whatever was on the admin, either blank or another category... Ok now Ill just have to add either a field that shows the categories... or modify the template so that it shows the categories added in the post save... what would you recommend THANK YOU... – maumercado Mar 17 '11 at 23:05
  • Hmm, that's a tough one. I'm not sure what I would do.. Are the categories read only? Are you allowing people to override them? If not, you could very easily exclude the field and define a method in the ModelAdmin as a field that renders the categories. If you're setting the categories on every save, the user can't change them, so it doesn't sound like much of a problem. It might even be more confusing to have the original widget there where you can select choices / not have them applied! – Yuji 'Tomita' Tomita Mar 17 '11 at 23:14
  • Im going to try and render them as read only since each product is being autocategorized... but im going to try and render it inside the instace instead of the all list... Thank you so much... – maumercado Mar 17 '11 at 23:19