This package allows you to set relation from API request by primary key, object's dict, list of PKs, list of dicts of mixed list.
For example
{
"name": "Quentin Tarantino",
"person_type": {"id": 10, "name": "Director"}, // dict (field ID is only required)
"city": 15, // primary key
"movies": [ // list
15431, // can be primary key
{"id": 31123}, // can be dict
{"id": 21100, "name": "Pulp Fiction"}
]
}
RelatedField
is class implemented of PrimaryKeyRelatedField
.
It has:
serializer
Serializer class used for serializing objects (list, retrieve...)fail_on_not_found
Raise NotFound exception if object not foundextra_filter
Extra fields added to.filter()
class PersonSerializer(ModelSerializer):
person_type = RelatedField(serializer=TypeSerializer)
city = RelatedField(serializer=CitySerializer, required=False, fail_on_null=False, fail_on_not_found=False, extra_filter={'country': Country.objects.filter(name='USA')})
movies = RelatedField(serializer=MovieSerilizer, many=True)
class Meta(object):
model = Person
fields = ('id', 'name', 'person_type', 'city', 'movies')
get_related_object(search, model, fail_on_null=True, create_on_null=False, fail_on_not_found=True, extra_filter=None)
This function searches model
instance by search
(which would be primary key, dict with "id" field or list).
Attributes:
search
Primary key, dict with "id" field or list of primary keysmodel
Model to search infail_on_null
*Raise ValidationError if search is None or incorrectfail_on_not_found
Raise ValidationError if an instance was now found (does not work with list)create_on_null
Try to create a new objectextra_filter
Extra fields added to.filter()
get_relation_from_request(request, key: str, data: dict, model, fail_on_empty=False, fail_on_null=True,
fail_on_not_found=True, create_on_null=False, types=None, extra_filter=None)
This function gets value from request
, search instance by get_related_object()
and put it to data
.
Attributes:
request
HttpRequestkey
Key to get value from request datadata
Dict to put gotten instance inmodel
Model to search infail_on_empty
Raise ValidationError if there is nokey
inrequest
fail_on_null
*Raise ValidationError if search is None or incorrectfail_on_not_found
Raise ValidationError if an instance was now found (does not work with list)create_on_null
Try to create a new objecttypes
List of allowed value typesextra_filter
Extra fields added to.filter()
How to use it:
def perform_create(self, serializer):
data = dict()
get_relation_from_request(self.request, 'person_type', data, PersonType)
get_relation_from_request(self.request, 'city', data, City, fail_on_empty=False,
extra_fields={'country': Country.objects.filter(name='USA')})
get_relation_from_request(self.request, 'movies', data, Movie)
serializer.save(**data)