Skip to content

Allow referencing a type that doesn't exist yet

Yoann Schneider requested to merge non-existing-worker-types into master

Closes #1035 (closed)

The SlugRelatedField seems to be designed to reference objects that already exist.

def to_internal_value(self, data):
        queryset = self.get_queryset()
        try:
            return queryset.get(**{self.slug_field: data})
        except ObjectDoesNotExist:
            self.fail('does_not_exist', slug_name=self.slug_field, value=smart_str(data))
        except (TypeError, ValueError):
            self.fail('invalid')

If it doesn't exist yet, it simply fails. I used the regular SlugField with validation that uses get_or_create instead.

Edited by Yoann Schneider

Merge request reports

Loading