Skip to content

Commit 985852a

Browse files
authored
Merge pull request #333 from yezyilomo/release_v0.17.0
Release v0.17.0
2 parents 7d16406 + 53df479 commit 985852a

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ Full documentation for this project is available at [https://yezyilomo.github.io
150150
`python runtests.py`
151151

152152

153+
## Writing & Deploying Docs
154+
Run `pip3 install mkdocs-material` to install mkdocs-material
155+
156+
Run `mkdocs serve` to serve docs locally
157+
158+
Run `mkdocs gh-deploy --force` to deploy docs to gh-page
159+
160+
153161
## Credits
154162
* Implementation of this library is based on the idea behind [GraphQL](https://graphql.org/).
155163
* My intention is to extend the capability of [drf-dynamic-fields](https://github.com/dbrgn/drf-dynamic-fields) library to support more functionalities like allowing to query nested fields both flat and iterable at any level and allow writing on nested fields while maintaining simplicity.

django_restql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__title__ = 'Django RESTQL'
22
__description__ = 'Turn your API made with Django REST Framework(DRF) into a GraphQL like API.'
33
__url__ = 'https://yezyilomo.github.io/django-restql'
4-
__version__ = '0.16.2'
4+
__version__ = '0.17.0'
55
__author__ = 'Yezy Ilomo'
66
__author_email__ = '[email protected]'
77
__license__ = 'MIT'

docs/mutating_data.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,75 @@ This will remove all books associated with a course being updated.
447447
<br>
448448

449449

450+
### delete_on_null kwarg
451+
When dealing with nested fields, there are scenarios where the previously assigned object or resource is no longer needed after the field is cleared (i.e. set to `null`). In such cases, passing `delete_on_null=True` kwarg enables automatic deletion of the previously assigned resource when the nested field is explicitly updated to `null`.
452+
453+
This keyword argument applies only to `ForeignKey` or `OneToOne` relationships.
454+
The default value for `delete_on_null` kwarg is `False`.
455+
456+
Below is an example showing how to use `delete_on_null` kwarg.
457+
```py
458+
from rest_framework import serializers
459+
from django_restql.fields import NestedField
460+
from django_restql.serializers import NestedModelSerializer
461+
462+
from app.models import Location, Property
463+
464+
465+
class LocationSerializer(serializers.ModelSerializer):
466+
class Meta:
467+
model = Location
468+
fields = ["id", "city", "country"]
469+
470+
471+
class PropertySerializer(NestedModelSerializer):
472+
location = NestedField(LocationSerializer, delete_on_null=True)
473+
class Meta:
474+
model = Property
475+
fields = [
476+
'id', 'price', 'location'
477+
]
478+
```
479+
480+
Assuming we have a property with this structure
481+
```js
482+
{
483+
"id": 1,
484+
"price": 30000,
485+
"location": {
486+
"id": 5,
487+
"city": "Arusha",
488+
"country": "Tanzania"
489+
}
490+
}
491+
```
492+
493+
Sending a mutation request to update this property by removing a location
494+
495+
```PUT/PATCH /api/property/1/```
496+
497+
Request Body
498+
```js
499+
{
500+
"location": null
501+
}
502+
```
503+
504+
Response
505+
```js
506+
{
507+
"id": 1,
508+
"price": 30000,
509+
"location": null
510+
}
511+
```
512+
513+
In this case, the property’s location is updated to null, and the previously assigned Location instance (with id: 5) is deleted from the database.
514+
515+
!!! note
516+
`delete_on_null=True` can only be used when both `accept_pk=False` and `accept_pk_only=False`. This is because `accept_pk=True` or `accept_pk_only=True` typically implies that the nested object is not tightly coupled to the parent and may be referenced elsewhere. Automatically deleting it in such cases could lead to unintended side effects or broken references.
517+
518+
450519
## Using DynamicFieldsMixin and NestedField together
451520
You can use `DynamicFieldsMixin` and `NestedModelSerializer` together if you want your serializer to be writable(on nested fields) and support querying data, this is very common. Below is an example which shows how you can use `DynamicFieldsMixin` and `NestedField` together.
452521

0 commit comments

Comments
 (0)