0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

個人開発記録 2024/04/22

Posted at

目的

  • とにかくやりきることが一番の目的
  • 毎日発信することで、発信する習慣作り
  • 個人開発をやりきることで、実績作り

開発するもの

X(旧Twitter)のパクリ

構成

python
django REST framework

本編

昨日までのものは中途半端だったため、再度作り直し。

Python環境に、venvにより仮想環境を構築します。

cd likeX
python -m venv venv

djangoをインストール

pip install django
pip install djangorestframework

djangoプロジェクトを作成

django-admin startproject likeX .

アプリを作成

cd likeX
django-admin startapp tweet
cd ..

マイグレーションを実行

python manage.py migrate
python manage.py createsuperuser --username admin --email admin@example.com
likeX/tweet/serializers.py
from django.contrib.auth.models import Group, User
from rest_framework import serializers


class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ['url', 'username', 'email', 'groups']


class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Group
        fields = ['url', 'name']

likeX/tweet/views.py
from django.contrib.auth.models import Group, User
from rest_framework import permissions, viewsets

from .serializers import GroupSerializer, UserSerializer


class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated]


class GroupViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows groups to be viewed or edited.
    """
    queryset = Group.objects.all().order_by('name')
    serializer_class = GroupSerializer
    permission_classes = [permissions.IsAuthenticated]

likeX/urls.py
from django.urls import include, path
from rest_framework import routers

from likeX.tweet import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    path('', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

likeX/settings.py
REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10
}

以下を修正

likeX/settings.py
INSTALLED_APPS = [
    ...
    'rest_framework',
]

起動

python manage.py runserver

APIを実行
以下コマンドで作成した、ユーザの情報が取れる
python manage.py createsuperuser --username admin --email admin@example.com

curl -u admin -H 'Accept: application/json; indent=4' http://127.0.0.1:8000/users/
Enter host password for user 'admin':
{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [
        {
            "url": "http://127.0.0.1:8000/users/1/",
            "username": "admin",
            "email": "admin@example.com",
            "groups": []
        }
    ]
}

感想

ほぼチュートリアルどおり作成。
チュートリアル通り作成してるのに動かなかったりするのでちょっと困った。
かなり雑な記事になったので、後日整理。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?