2

django-taggit==1.2.0

Django==3.0.8

class TagMixin(models.Model):
    tags = TaggableManager(verbose_name="Tags") # django-taggit

    class Meta:
        abstract = True


class Post(TagMixin,
           models.Model):

    category = models.ForeignKey(Category,
                                 on_delete=models.PROTECT,
                                 verbose_name="Category")
    def is_featured(self):
        return SPECIAL_TAGS.FEATURED.value in self.tags.names()

    def is_news(self):
        return self.category.slug == SPECIAL_CATEGORIES.NEWS.value

    def _validate_featured(self):
            if not self.is_news() and self.is_featured():
                raise ValidationError("Featured can be in News only.")

Problem

In the admin site there is Tags field visible. But really it is not a field. It is a manager.

Therefore my validator doesn't work properly: it doesn't validate tags the field in the admin site.

What is_featured() does is extracting tag names from the database.

How can I validate what tags are input by a user? The validation requires access to more than a just tags (as you can see, it implies interdependency between tags and categories).

halfer
  • 19,824
  • 17
  • 99
  • 186
Michael
  • 4,273
  • 3
  • 40
  • 69

0 Answers0