def __init__(self, *args, **kwargs): DefaultModelSerializer.__init__(self, *args, **kwargs) # Cache the name of the primary key self.pk = self.Meta.model._meta.pk.name.lower() # Cache the natural key if hasattr(self.Meta.model.objects, "get_by_natural_key"): if self.Meta.model._meta.unique_together: self.natural_key = self.Meta.model._meta.unique_together[0] elif hasattr(self.Meta.model, "natural_key") and isinstance( self.Meta.model.natural_key, tuple): self.natural_key = self.Meta.model.natural_key else: self.natural_key = None else: self.natural_key = None # Strip out the uniqueness validators on primary keys. # We don't need them with our find-or-create feature. for i in self.fields: if i == self.pk or (self.natural_key and i in self.natural_key): self.fields[i].required = False if i == self.pk: self.fields[i].validators = [ i for i in self.fields[i].validators if not isinstance(i, UniqueValidator) ] # Strip out the uniqueness validators on natural keys. # We don't need them with our find-or-create feature. self.validators = [ i for i in self.validators if not isinstance(i, UniqueTogetherValidator) or not i.fields == self.natural_key ]
def __init__(self, *args, **kwargs): ModelSerializer.__init__(self, *args, **kwargs) self.fields['typified_value'].source = 'value' self.fields['typified_value'].field_name = 'value'