-1

I have a category kind of model (called Tag):

class Tag(models.Model):
    name = models.CharField(...)
    slug = models.SlugField(unique=True)
    #...

Used as m2m field in another model:

class Order(models.Model):
    user = models.ForeignKey(...)
    type = models.CharField(...)
    tag = models.ManyToManyField(Tag, blank=True)

Now, suppose an order is created and program wants to assign some tags to it automatically. Say, program wants to add Order.type to itsOrder.tag list. (Suppose Order.type happens to be same as a valid Tag.name) I tried Order's post_save to no luck:

def order_post_save_reciever(sender, instance, *args, **kwargs):
    #..., disconnect, ...
    instance.tag.add(Tag.objects.filter(name=instance.type))
    instance.save()
    #.... reconnec, t.....

Apparently, I have to use signals, but I don't know how. Do you know how to add a tag here? Your help is much appreciated.

rahman
  • 4,820
  • 16
  • 52
  • 86
  • Why do you have to use signals? Are not both models in your own project? Also, the answer you linked you Apparently haven't read, because it explicitly states not to use `post_save`. –  Dec 05 '20 at 10:29
  • @Melvyn maybe I haven't explained it correctly. Yes, I read the link and realized `post_save` is NOT the correct place. Moreover, I don't know if/how I should use the signal. That is why I asked :) . I guess it is you who didn't read the question correctly. What is your solution? (if you know any). That is what we are seeking after all. – rahman Dec 05 '20 at 15:53
  • My solution is not to use signals, if both my are my models. If you save an Order, you can add a tag in its save method. Why use signals? In fact, if Order is my model, I don't care who's model Tag is, I don't have to use signals. –  Dec 05 '20 at 17:31

1 Answers1

0

Remove instance.save() as it'll create a loop.

The Mask
  • 390
  • 3
  • 17