How do I create a serializer that reuses a unique key of my model?
问题 I'm using Python 3.7, Django 2.2, the Django rest framework, and pytest. I have the following model, in which I want to re-use an existing model if it exists by its unique key ... class CoopTypeManager(models.Manager): def get_by_natural_key(self, name): return self.get_or_create(name=name)[0] class CoopType(models.Model): name = models.CharField(max_length=200, null=False, unique=True) objects = CoopTypeManager() Then I have created the below serializer to generate this model from REST data