I have a model, say, Ticket
. The Ticket
has Tag
as ManyToMany field among the other fields.
After somebody updates any field of Ticket
(say Description
), I want to set the Tag
field value to one of the existing tags.
How do I do this?
I have a model, say, Ticket
. The Ticket
has Tag
as ManyToMany field among the other fields.
After somebody updates any field of Ticket
(say Description
), I want to set the Tag
field value to one of the existing tags.
How do I do this?
Use Django signals
@receiver(post_save, sender=Ticket)
def create_tag(sender, instance, created, **kwargs):
if not created:
instance.tags.add(Tag.objects.get(id=1)) #Add an existing tag
instance.save()
post_save.connect(create_tag, sender=Ticket)