add natural key support to ResponseType

Have the option to use the more readable response type value rather than
the ResponseType id integer in fixtures and dumpdata output.

Prior to this change dumpdata represents response types like so:

    "response_types": [2]

And after this change when using `dumpdata --natural-foreign`:

    "response_types": [["code"]]
This commit is contained in:
Andy Clayton 2018-08-20 10:11:28 -05:00
parent 436568a39b
commit 4e7116ca9e

View file

@ -30,7 +30,14 @@ JWT_ALGS = [
]
class ResponseTypeManager(models.Manager):
def get_by_natural_key(self, value):
return self.get(value=value)
class ResponseType(models.Model):
objects = ResponseTypeManager()
value = models.CharField(
max_length=30,
choices=RESPONSE_TYPE_CHOICES,
@ -40,6 +47,9 @@ class ResponseType(models.Model):
max_length=50,
)
def natural_key(self):
return self.value, # natural_key must return tuple
def __str__(self):
return u'{0}'.format(self.description)