Metadata generator for Django Rest Framework
Project description
Installation
pip install -e git+https://github.com/night-crawler/drf-metadata.git@#egg=drf-metadata
Models
class Author(models.Model):
name = models.CharField(max_length=255)
birth = models.DateField('birth date')
def __str__(self):
return self.name
class Publisher(models.Model):
name = models.CharField(max_length=255)
state = models.PositiveSmallIntegerField(
'publisher state',
choices=(
(0, 'Active'),
(1, 'Disabled')
),
default=1
)
def __str__(self):
return self.name
class Book(models.Model):
title = models.CharField(max_length=255)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
Usage
from drf_metadata.meta import MetaData, AbstractField, CustomMetadata
class BookMetadata(MetaData):
model = Book
serializers = {
'publisher': PublisherSerializer
}
update_fields = {
'authors': {
'omg': {'lol': 1}
}
}
# skip serialization (don't include model instances to choice field)
dataset_urls = {
'authors': '/author/',
'publisher': '/publisher/',
}
# custom queryset for instance (self has `obj` if you need to filter qs)
def get_authors_queryset(self, field):
return Author.objects.filter(name='author0')
# use custom serializer for field instance(s)
def get_authors_serializer(self, field):
# self.request, self.view, self.obj are available in MetaData instance
return AuthorSerializer
# update field bundle with runtime values
def update_authors_field_meta(field, obj):
return {'new': 1, 'obj': str(obj)}
# use own serializer
def get_publisher_field_meta(field, obj):
return {'new': 1, 'obj': str(obj)}
Usage with django-rest-framework
# or redefine OPTIONS handler
class BookViewSet(viewsets.ReadOnlyModelViewSet):
@list_route()
def describe_book(self, request):
md = metadata.BookMetadata().determine_metadata(request, self)
return Response(md)
Sample response
{
"title": "book", "description": "description",
"fields": [
{
"type": "CharField", "max_length": 255,
"name": "title",
"verbose_name": "title",
"help_text": "", "blank": false, "null": false, "editable": true, "required": true},
{
"type": "ForeignKey",
"name": "publisher", "verbose_name": "publisher", "help_text": "",
"blank": false, "null": false, "editable": true, "required": true,
"data": [
{"id": 47, "name": "pub0"},
{"id": 48, "name": "pub1"},
{"id": 49, "name": "pub2"}
]
},
{
"type": "ManyToManyField",
"name": "authors", "verbose_name": "authors", "help_text": "",
"blank": false, "null": false, "editable": true, "required": true,
"data": [
{"id": 37, "name": "author0"},
{"id": 38, "name": "author1"},
{"id": 39, "name": "author2"}
]
}
]
}
Non-model MetaData
class ImpersonateMetadata(CustomMetadata):
title = 'View site as another user'
action_name = 'Impersonate'
fields = (
AbstractField(type='ForeignKey',
name='user_id',
verbose_name='User',
data='/data/',
required=True),
)
# method fields
def get_superfield(self, request):
return {
'name': 'hero',
'super': True
}
def get_lol(self, request):
return {
'name': 'lol',
'super': False
}
# update field `user_id`
def get_field_user_id(self, field_name, request):
return {
'lol': 1
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
drf-metadata-0.1.12.tar.gz
(10.0 kB
view details)
Built Distribution
File details
Details for the file drf-metadata-0.1.12.tar.gz
.
File metadata
- Download URL: drf-metadata-0.1.12.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dfcd4b0760972be360c012fb7fccd900026be0c752f87ba2a326d1164e62b7e9 |
|
MD5 | d7f4b9ac8e22057b8ab61c5991723031 |
|
BLAKE2b-256 | 16b80fe7d0046fd9e6c10715ed9cdb55778fc705bd448d5820312b78cbf1c512 |
File details
Details for the file drf_metadata-0.1.12-py3-none-any.whl
.
File metadata
- Download URL: drf_metadata-0.1.12-py3-none-any.whl
- Upload date:
- Size: 14.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3146350e73f69046406a8049b4eb984e384a74eab6be04920a5042b29e57da3 |
|
MD5 | c4f51da7b4830144ae7eae56f51ba529 |
|
BLAKE2b-256 | d65267b560912a97cb2c3cc9585d559771233fc408833fad4d3054582a994a39 |