Search code examples
djangodjango-taggitwagtail

Django-Taggit validate newly added tags before save


This is how my models look like. I was not able to do the validation by overriding the save method of either of the models.

class NotificationListTag(TaggedItemBase):
    content_object = models.ForeignKey('Mailer')


class Mailer(models.Model):

    from_email = models.CharField(max_length=255)
    to_emails = TaggableManager(
        through=NotificationListTag,
        blank=True,
        verbose_name='To Emails',
        help_text='Space separated email ID'
    )

How do I validate each of the newly added tags before it gets saved. Did I miss something out.


Solution

  • I ended up creating a custom API for the tagging autocomplete suggestions, that only suggests valid names.

    You can override the the tagit.js call wagtail makes by default, to pass your custom autocomplete URL to it. In this way, you don't have to do validation after the user has entered a value.