manosim / manosim/django-rest-framework-docs
setting the fields description if you are using function based views
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 641
- Forks
- 178
- PR merge metrics
- No merged PRs in 30d
Description
How do I set the field descriptions in views.py on function based views?
**This is my syntax:**
@api_view(['GET', 'POST'])
def api_section(request):
"""
GET request gets the section and its details..... POST request, adds a new section
"""
if request.method == 'GET':
sections = Section.objects.all()
serializer = SectionSerializer(sections, many=True)
return Response(serializer.data)
```
elif request.method == 'POST':
serializer = SectionSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
serializer_data = serializer.data
del serializer_data['id']
return Response(serializer_data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
```
Field descriptions seems to work smoothly by declaring a serializer_class variable in class-based views that inherits APIView like this one:
class ObtainAuthToken(APIView):
throttle_classes = ()
permission_classes = ()
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
renderer_classes = (renderers.JSONRenderer,)
serializer_class = AuthTokenSerializer
```
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
token, created = Token.objects.get_or_create(user=user)
return Response({'token': token.key})
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the views.py function-based example using @api_view and compare it with the APIView serializer_class example in the issue. Review the relevant documentation entry points and determine what guidance is needed for field descriptions in function-based views; done means the supported approach is documented clearly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, python
- Domain
- api, backend, documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100