Skip to content

UniqueValidator raises exception for nested objects #2403

Closed
@gregorjerse

Description

@gregorjerse

I have a model Service which has contains a ForeignKey to atSifrantStoritev_model.

class Service(RemarkModel):
    code = ForeignKey(tSifrantStoritev_model, to_field='SiSto', related_name='services', verbose_name=_(u'The code of the given service.'), help_text=_(u'The unique code given to the given service, usually by health insurance companies.'))

class tSifrantStoritev_model(models.Model):
    class Meta:
        verbose_name = _(u"tSifSto_mo")
    # Unique added by Gregor for index
    # SiSto is referenced from Racuni.models.Service as Foreign key
    SiSto = models.CharField(max_length=1000, blank=True, unique=True) 

Using ModelViewSet for Service andatSifrantStoritev_model models the update for the object of type Service fails with the folloving data.

dict: {'code': {'SiSto': [u'This field must be unique.']}}

I traced the source of the exception to the function

    def exclude_current_instance(self, queryset):
        """
        If an instance is being updated, then do not include
        that instance itself as a uniqueness conflict.
        """
        if self.instance is not None:
            return queryset.exclude(pk=self.instance.pk)
        return queryset

in the validators.py file. The method fails to remove a currenttSifrantStoritev_model object since self.instance equals to None. Could this be a bug in Django Rest validation system or am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions