How to validate uniqueness constraint across foreign key (django)

后端 未结 3 1063
感动是毒
感动是毒 2020-12-03 10:47

I have the following (simplified) data structure:

Site
-> Zone
   -> Room
      -> name

I want the name of each Room to be unique

相关标签:
3条回答
  • 2020-12-03 11:21

    Methods are not called on their own when saving the model. One way to do this is to have custom save method that calls the validate_unique method when model is saved:

    class Room(models.Model):
        zone = models.ForeignKey(Zone)
        name = models.CharField(max_length=255) 
    
        def validate_unique(self, exclude=None):
            qs = Room.objects.filter(name=self.name)
            if qs.filter(zone__site=self.zone__site).exists():
                raise ValidationError('Name must be unique per site')
    
    
        def save(self, *args, **kwargs):
    
            self.validate_unique()
    
            super(Room, self).save(*args, **kwargs)
    
    0 讨论(0)
  • The Django Validation objects documentation explains the steps involved in validation including this snippet

    Note that full_clean() will not be called automatically when you call your model's save() method

    If the model instance is being created as a result of using a ModelForm, then validation will occur when the form is validated.

    There are a some options in how you handle validation.

    1. Call the model instance's full_clean() manually before saving.
    2. Override the save() method of the model to perform validation on every save. You can choose how much validation should occur here, whether you want full validation or only uniqueness checks.

      class Room(models.Model):
          def save(self, *args, **kwargs):
              self.full_clean()
              super(Room, self).save(*args, **kwargs)
      
    3. Use a Django pre_save signal handler which will automatically perform validation before a save. This provides a very simple way to add validation on exisiting models without any additional model code.

      # In your models.py
      from django.db.models.signals import pre_save
      
      def validate_model_signal_handler(sender, **kwargs):
          """
          Signal handler to validate a model before it is saved to database.
          """
          # Ignore raw saves.
          if not kwargs.get('raw', False):
              kwargs['instance'].full_clean()
      
      
      pre_save.connect(validate_model_signal_handler,
        sender=Room,
        dispatch_uid='validate_model_room')
      
    0 讨论(0)
  • 2020-12-03 11:38
    class Room(models.Model):
        zone = models.ForeignKey(Zone)
        name = models.CharField(max_length=255)
    
        def validate_unique(self, *args, **kwargs):
            super(Room, self).validate_unique(*args, **kwargs)
            qs = Room.objects.filter(name=self.name)
            if qs.filter(zone__site=self.zone__site).exists():
                raise ValidationError({'name':['Name must be unique per site',]})
    

    I needed to make similar program. It worked.

    0 讨论(0)
提交回复
热议问题