Question

I have a field title in a model Skill inheriting from CMSPlugin. I am using the following properties of my Skill objects as a unique identifier:

  • title
  • language
  • placeholder.page.publisher_is_draft (weather the corresponding page is published or draft)

I would like to prevent users from creating two instances where these three properties are equal. It seems I cannot achieve this with a unique_together definition, as the third property is not part of the model. Is there a built-in mechanism in the Django CMS to define such a constraint?

Était-ce utile?

La solution

Sure thing, this is what I gathered from your question.

from django.core.exceptions import ValidationError

class MyPlugin(CMSPlugin):
    title = models.CharField(max_length=200)
    is_draft = models.BooleanField(default=False)

    def clean(self):
        manager = self.__class__.objects
        self.is_draft = self.placeholder.page.publisher_is_draft
        if manager.filter(title=self.title, language=self.language, is_draft=self.is_draft).exists():
            raise ValidationError("Duplicate !!!")
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top