mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-07 05:50:13 +03:00
Merge branch 'master' of github.com:tomchristie/django-rest-framework
This commit is contained in:
commit
ecdc3b3a6b
|
@ -432,6 +432,10 @@ The [djangorestframework-word-filter][django-rest-framework-word-search-filter]
|
|||
|
||||
[django-url-filter][django-url-filter] provides a safe way to filter data via human-friendly URLs. It works very similar to DRF serializers and fields in a sense that they can be nested except they are called filtersets and filters. That provides easy way to filter related data. Also this library is generic-purpose so it can be used to filter other sources of data and not only Django `QuerySet`s.
|
||||
|
||||
## drf-url-filters
|
||||
|
||||
[drf-url-filter][drf-url-filter] is a simple Django app to apply filters on drf `ModelViewSet`'s `Queryset` in a clean, simple and configurable way. It also supports validations on incoming query params and their values. A beautiful python package `Voluptouos` is being used for validations on the incoming query parameters. The best part about voluptouos is you can define your own validations as per your query params requirements.
|
||||
|
||||
[cite]: https://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-specific-objects-with-filters
|
||||
[django-filter]: https://github.com/alex/django-filter
|
||||
[django-filter-docs]: https://django-filter.readthedocs.io/en/latest/index.html
|
||||
|
@ -443,3 +447,5 @@ The [djangorestframework-word-filter][django-rest-framework-word-search-filter]
|
|||
[django-rest-framework-filters]: https://github.com/philipn/django-rest-framework-filters
|
||||
[django-rest-framework-word-search-filter]: https://github.com/trollknurr/django-rest-framework-word-search-filter
|
||||
[django-url-filter]: https://github.com/miki725/django-url-filter
|
||||
[drf-url-filter]: https://github.com/manjitkumar/drf-url-filters
|
||||
|
||||
|
|
|
@ -286,7 +286,7 @@ Would serialize to a nested representation like this:
|
|||
],
|
||||
}
|
||||
|
||||
# Writable nested serializers
|
||||
## Writable nested serializers
|
||||
|
||||
By default nested serializers are read-only. If you want to support write-operations to a nested serializer field you'll need to create `create()` and/or `update()` methods in order to explicitly specify how the child relationships should be saved.
|
||||
|
||||
|
@ -324,8 +324,14 @@ By default nested serializers are read-only. If you want to support write-operat
|
|||
>>> serializer.save()
|
||||
<Album: Album object>
|
||||
|
||||
---
|
||||
|
||||
# Custom relational fields
|
||||
|
||||
In rare cases where none of the existing relational styles fit the representation you need,
|
||||
you can implement a completely custom relational field, that describes exactly how the
|
||||
output representation should be generated from the model instance.
|
||||
|
||||
To implement a custom relational field, you should override `RelatedField`, and implement the `.to_representation(self, value)` method. This method takes the target of the field as the `value` argument, and should return the representation that should be used to serialize the target. The `value` argument will typically be a model instance.
|
||||
|
||||
If you want to implement a read-write relational field, you must also implement the `.to_internal_value(self, data)` method.
|
||||
|
|
|
@ -1089,6 +1089,7 @@ The following third party packages are also available.
|
|||
The [django-rest-marshmallow][django-rest-marshmallow] package provides an alternative implementation for serializers, using the python [marshmallow][marshmallow] library. It exposes the same API as the REST framework serializers, and can be used as a drop-in replacement in some use-cases.
|
||||
|
||||
## Serpy
|
||||
|
||||
The [serpy][serpy] package is an alternative implementation for serializers that is built for speed. [Serpy][serpy] serializes complex datatypes to simple native types. The native types can be easily converted to JSON or any other format needed.
|
||||
|
||||
## MongoengineModelSerializer
|
||||
|
@ -1107,7 +1108,12 @@ The [django-rest-framework-hstore][django-rest-framework-hstore] package provide
|
|||
|
||||
The [dynamic-rest][dynamic-rest] package extends the ModelSerializer and ModelViewSet interfaces, adding API query parameters for filtering, sorting, and including / excluding all fields and relationships defined by your serializers.
|
||||
|
||||
## Dynamic Fields Mixin
|
||||
|
||||
The [drf-dynamic-fields][drf-dynamic-fields] package provides a mixin to dynamically limit the fields per serializer to a subset specified by an URL parameter.
|
||||
|
||||
## HTML JSON Forms
|
||||
|
||||
The [html-json-forms][html-json-forms] package provides an algorithm and serializer for processing `<form>` submissions per the (inactive) [HTML JSON Form specification][json-form-spec]. The serializer facilitates processing of arbitrarily nested JSON structures within HTML. For example, `<input name="items[0][id]" value="5">` will be interpreted as `{"items": [{"id": "5"}]}`.
|
||||
|
||||
[cite]: https://groups.google.com/d/topic/django-users/sVFaOfQi4wY/discussion
|
||||
|
@ -1124,3 +1130,4 @@ The [html-json-forms][html-json-forms] package provides an algorithm and seriali
|
|||
[dynamic-rest]: https://github.com/AltSchool/dynamic-rest
|
||||
[html-json-forms]: https://github.com/wq/html-json-forms
|
||||
[json-form-spec]: https://www.w3.org/TR/html-json-forms/
|
||||
[drf-dynamic-fields]: https://github.com/dbrgn/drf-dynamic-fields
|
||||
|
|
|
@ -61,6 +61,7 @@ It takes a single required argument, and an optional `messages` argument:
|
|||
|
||||
* `queryset` *required* - This is the queryset against which uniqueness should be enforced.
|
||||
* `message` - The error message that should be used when validation fails.
|
||||
* `lookup` - The lookup used to find an existing instance with the value being validated. Defaults to `'exact'`.
|
||||
|
||||
This validator should be applied to *serializer fields*, like so:
|
||||
|
||||
|
|
|
@ -42,10 +42,11 @@ class UniqueValidator(object):
|
|||
"""
|
||||
message = _('This field must be unique.')
|
||||
|
||||
def __init__(self, queryset, message=None):
|
||||
def __init__(self, queryset, message=None, lookup='exact'):
|
||||
self.queryset = queryset
|
||||
self.serializer_field = None
|
||||
self.message = message or self.message
|
||||
self.lookup = lookup
|
||||
|
||||
def set_context(self, serializer_field):
|
||||
"""
|
||||
|
@ -62,7 +63,7 @@ class UniqueValidator(object):
|
|||
"""
|
||||
Filter the queryset to all instances matching the given attribute.
|
||||
"""
|
||||
filter_kwargs = {self.field_name: value}
|
||||
filter_kwargs = {'%s__%s' % (self.field_name, self.lookup): value}
|
||||
return qs_filter(queryset, **filter_kwargs)
|
||||
|
||||
def exclude_current_instance(self, queryset):
|
||||
|
|
|
@ -31,7 +31,7 @@ class RelatedModel(models.Model):
|
|||
|
||||
class RelatedModelSerializer(serializers.ModelSerializer):
|
||||
username = serializers.CharField(source='user.username',
|
||||
validators=[UniqueValidator(queryset=UniquenessModel.objects.all())]) # NOQA
|
||||
validators=[UniqueValidator(queryset=UniquenessModel.objects.all(), lookup='iexact')]) # NOQA
|
||||
|
||||
class Meta:
|
||||
model = RelatedModel
|
||||
|
@ -103,7 +103,7 @@ class TestUniquenessValidation(TestCase):
|
|||
AnotherUniquenessModel._meta.get_field('code').validators, [])
|
||||
|
||||
def test_related_model_is_unique(self):
|
||||
data = {'username': 'existing', 'email': 'new-email@example.com'}
|
||||
data = {'username': 'Existing', 'email': 'new-email@example.com'}
|
||||
rs = RelatedModelSerializer(data=data)
|
||||
self.assertFalse(rs.is_valid())
|
||||
self.assertEqual(rs.errors,
|
||||
|
|
2
tox.ini
2
tox.ini
|
@ -17,7 +17,7 @@ setenv =
|
|||
deps =
|
||||
django18: Django==1.8.15
|
||||
django19: Django==1.9.10
|
||||
django110: Django==1.10
|
||||
django110: Django==1.10.2
|
||||
djangomaster: https://github.com/django/django/archive/master.tar.gz
|
||||
-rrequirements/requirements-testing.txt
|
||||
-rrequirements/requirements-optionals.txt
|
||||
|
|
Loading…
Reference in New Issue
Block a user