Improved interaction with DRF relations.
Project description
DRF Improved Relations
What does it do?
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"}
]
}
How to use it?
RelatedField
RelatedField is class implemented of PrimaryKeyRelatedField.
It has:
serializerSerializer class used for serializing objects (list, retrieve...)fail_on_not_foundRaise NotFound exception if object not foundextra_filterExtra 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
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:
searchPrimary key, dict with "id" field or list of primary keysmodelModel to search infail_on_null*Raise ValidationError if search is None or incorrectfail_on_not_foundRaise ValidationError if an instance was now found (does not work with list)create_on_nullTry to create a new objectextra_filterExtra fields added to.filter()
get_relation_from_request
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:
requestHttpRequestkeyKey to get value from request datadataDict to put gotten instance inmodelModel to search infail_on_emptyRaise ValidationError if there is nokeyinrequestfail_on_null*Raise ValidationError if search is None or incorrectfail_on_not_foundRaise ValidationError if an instance was now found (does not work with list)create_on_nullTry to create a new objecttypesList of allowed value typesextra_filterExtra 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)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
drf_irelation-0.0.6.tar.gz
(3.8 kB
view details)
File details
Details for the file drf_irelation-0.0.6.tar.gz.
File metadata
- Download URL: drf_irelation-0.0.6.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2cfb46d4d7112786cea339f2d2f824da0fc5967ecc893bd2a3d46aa4db2a6290
|
|
| MD5 |
d872a1bb65cc2487fbf5b5b8caed992d
|
|
| BLAKE2b-256 |
93fdce14fb7136547ae59eeed225bd47c0b4b1d573e1db795c661a1f0711e3bc
|