vendredi 21 août 2020

django unique together just if

I want to use unique together just if the post_type is index 1. For every section to be possible to choose article type index 1 only one time but possible to chose index 0 with no limitation.

'''

 class Post(models.Model):
    Article_type = (('news', 'Het laatste nieuws'),
                    ('introduction', 'Een introductie'))
    post_id = models.AutoField(primary_key=True)
    post_title = models.CharField(max_length=50, null=False)
    post_header = models.TextField(max_length=250, null=True, blank=True)
    post_body = RichTextUploadingField(null=True)
    post_expire_date = models.DateTimeField(null=True, blank=True)
    post_available_date = models.DateTimeField(default=timezone.now, null=False)
    post_image = models.ImageField(upload_to='post-uploads/', null=True, blank=True)
    post_section = models.ForeignKey(Section, on_delete=models.CASCADE, default=1)
    post_type = models.CharField(max_length=30, choices=Article_type, default="news")
    post_author = models.ForeignKey(
        user,
        on_delete=models.CASCADE,
        limit_choices_to={'is_staff': True},
        null=True,
        default=user,
    )
    slug = models.SlugField(unique=True)

'''

Aucun commentaire:

Enregistrer un commentaire