Web APIs for Django with comments, made easy.
Project description
[I forked this from DRF]
Django REST framework
Awesome web-browsable Web APIs.
Full documentation for the project is available at https://www.django-rest-framework.org/.
Funding
REST framework is a collaboratively funded project. If you use REST framework commercially we strongly encourage you to invest in its continued development by signing up for a paid plan.
The initial aim is to provide a single full-time position on REST framework. Every single sign-up makes a significant impact towards making that possible.
Many thanks to all our wonderful sponsors, and in particular to our premium backers, Sentry, Stream, Spacinov, Retool, bit.io, PostHog, CryptAPI, FEZTO, and Svix.
Overview
Django REST framework is a powerful and flexible toolkit for building Web APIs.
Some reasons you might want to use REST framework:
- The Web browsable API is a huge usability win for your developers.
- Authentication policies including optional packages for OAuth1a and OAuth2.
- Serialization that supports both ORM and non-ORM data sources.
- Customizable all the way down - just use regular function-based views if you don't need the more powerful features.
- Extensive documentation, and great community support.
Below: Screenshot from the browsable API
Requirements
- Python 3.8+
- Django 5.0, 4.2
We highly recommend and only officially support the latest patch release of each Python and Django series.
Installation
Install using pip
...
pip install drf-comments
Add 'drf_comments'
to your INSTALLED_APPS
setting.
INSTALLED_APPS = [
...
'drf_comments',
]
Example
Let's take a look at a quick example of using REST framework to build a simple model-backed API for accessing users and groups.
Startup up a new project like so...
pip install django
pip install drf-comments
django-admin startproject example .
./manage.py migrate
./manage.py createsuperuser
Since this is a fork of django-rest-framework you do not need to install it unless this package is not up-to-date with the current django-rest-framework available
Now edit the example/urls.py
module in your project:
from django.db import models
from django.contrib.auth.models import User
from drf_comments import serializers, viewsets
from drf_comments.comments import CommentViewSet, create_comment_model_for, create_comment_create_serializer_for, create_comment_serializer_for, create_comment_viewset_for
from drf_comments.permissions import IsAuthenticated
from drf_comments.authentication import TokenAuthentication
# model to add comments to
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=255)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
#serializer
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ['id', 'user', 'title', 'content', 'created_at', 'updated_at']
#viewsets
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
permission_classes = [IsAuthenticated]
authentication_classes = [TokenAuthentication]
class PostCommentViewSet(CommentViewSet):
model = Post
comment_model = create_comment_model_for(Post)
serializer_class = create_comment_serializer_for(Post)
create_serializer_class = create_comment_create_serializer_for(Post)
permission_classes = [IsAuthenticated]
authentication_classes = [TokenAuthentication]
# Routers provide a way of automatically determining the URL conf.
router = DefaultRouter()
router.register(r'posts', PostViewSet)
router.register(r'posts/(?P<object_id>\d+)/comments', PostCommentViewSet, basename='post-comments')
# Wire up our API using automatic URL routing.
urlpatterns = [
path('', include(router.urls)),
]
We'd also like to configure a couple of settings for our API.
Add the following to your settings.py
module:
INSTALLED_APPS = [
... # Make sure to include the default installed apps here.
'drf_comments',
]
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'drf_comments.permissions.DjangoModelPermissionsOrAnonReadOnly',
]
}
That's it, we're done!
./manage.py runserver
You can now open the API in your browser at http://127.0.0.1:8000/
, and view your new 'users' API. If you use the Login
control in the top right corner you'll also be able to add, create and delete users from the system.
You can also interact with the API using command line tools such as curl
. For example:
$ curl -X POST http://localhost:8000/posts/ \
-H "Content-Type: application/json" \
-H "Authorization: Token YOUR_AUTH_TOKEN" \
-d '{
"user": 1,
"title": "First Post",
"content": "This is the content of the first post."
}'
And to create a comment tp that post:
$ curl -X POST http://localhost:8000/posts/1/comments/ \
-H "Content-Type: application/json" \
-H "Authorization: Token YOUR_AUTH_TOKEN" \
-d '{
"user": 1,
"text": "This is a comment on the first post."
}'
You can also use requests.py to make API calls like so:
import requests
url = "http://localhost:8000/posts/"
headers = {
"Content-Type": "application/json",
"Authorization": "Token YOUR_AUTH_TOKEN"
}
data = {
"user": 1,
"title": "First Post",
"content": "This is the content of the first post."
}
response = requests.post(url, headers=headers, json=data)
print(response.status_code)
print(response.json())
And:
import requests
url = "http://localhost:8000/posts/1/comments/"
headers = {
"Content-Type": "application/json",
"Authorization": "Token YOUR_AUTH_TOKEN"
}
data = {
"user": 1,
"text": "This is a comment on the first post."
}
response = requests.post(url, headers=headers, json=data)
print(response.status_code)
print(response.json())
Documentation & Support
Full documentation for the project is available at https://www.django-rest-framework.org/.
For questions and support, use the REST framework discussion group, or #restframework
on libera.chat IRC.
Security
Please see the security policy.
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
Built Distribution
File details
Details for the file drf-comments-0.6.tar.gz
.
File metadata
- Download URL: drf-comments-0.6.tar.gz
- Upload date:
- Size: 10.4 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a105bbc2e184985a059e858a1102c7d1a74b9cbfb001fcacf9dcc90169a9f688 |
|
MD5 | d6c4df9f3e734963f1cc70780ee856d2 |
|
BLAKE2b-256 | 75da56c2d5f2ee1815236681b6dfc57c0d0d09904ed835024b0842eea96124af |
File details
Details for the file drf_comments-0.6-py3-none-any.whl
.
File metadata
- Download URL: drf_comments-0.6-py3-none-any.whl
- Upload date:
- Size: 2.6 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff394ddcbe7bd9a8bd8553b760e875d533af1e11e49e0ada145217ee0ca56740 |
|
MD5 | bae2b8d12cb8e7591a10b662762e1ceb |
|
BLAKE2b-256 | 890b50caf48ca40a70e934e0fc6b40204f3383bf90a1cdc93ca0aedf302a3df7 |