python - Django Rest Framework end points -
I have a simple obj, it has three fields, one for a user created by the title of FK, Title, and Slug Gaya). The Slug field and user's Fif are unique together. I would like to create an API end point in which the urls base_url / api-model-name (retrieve all objs), base_url / api-model-name / username (this is used to create / list end point) Used for updating / retrieving Base_url / user name / obj-slug-obj for a user that has an FK that obj gives.
Any thoughts?
Using DRF, you can follow such a pattern.
models.py
class.fu (models.model): user = models.ForeignKey (user) title = models.CharField (MAX_LENGTH) = 100) Slug = models.SlugField (MAX_LENGTH = 50) class meta: unique_together = ('user', 'slug')
serializers.py
rest_framework import serializers import from .models from Foo Square FooSerializer (serializers.ModelSerializer): class meta model = foo
views.py
rest_framework.permissions Import from IsAuthenticated Import .serializers from .models Import_rames_widget.routers DefaultRouter Import from .groups = Foo.objects.all ()
routers.py
res .views , R'foos' FooViewSet) import from FooViewSet router = DefaultRouter () # registers for router.register URL
With this you would have the following endpoints:
get
/ foobars
- Retrieve all fobre objects Add a pk and view a description
POST
/ Foobars
- Fobre object `{" user ":" "," title ":" "," slug ":" "}
key Create other custom call you can add additional methods through DRF decorators (detail_route, list_route) Main ViewSet. But by following this method you can create a strong API.
Comments
Post a Comment