parent
d972386123
commit
947c5476c4
15 changed files with 105 additions and 3 deletions
@ -0,0 +1,21 @@ |
|||||||
|
# Generated by Django 3.1.3 on 2022-09-22 21:50 |
||||||
|
|
||||||
|
from django.conf import settings |
||||||
|
from django.db import migrations, models |
||||||
|
import django.db.models.deletion |
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration): |
||||||
|
|
||||||
|
dependencies = [ |
||||||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL), |
||||||
|
('article', '0001_initial'), |
||||||
|
] |
||||||
|
|
||||||
|
operations = [ |
||||||
|
migrations.AddField( |
||||||
|
model_name='article', |
||||||
|
name='author', |
||||||
|
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='articles', to=settings.AUTH_USER_MODEL), |
||||||
|
), |
||||||
|
] |
@ -0,0 +1,16 @@ |
|||||||
|
from rest_framework import permissions |
||||||
|
|
||||||
|
|
||||||
|
class IsAdminUserOrReadOnly(permissions.BasePermission): |
||||||
|
""" |
||||||
|
仅管理员用户可以进行修改 |
||||||
|
其他用户仅可进行查看 |
||||||
|
""" |
||||||
|
# 每次请求到来时被唤醒执行 |
||||||
|
def has_permission(self, request, view): |
||||||
|
# 对其他仅允许GET, HEAD, OPTIONS请求 |
||||||
|
if request.method in permissions.SAFE_METHODS: |
||||||
|
return True |
||||||
|
# 仅管理员可进行其他操作 |
||||||
|
return request.user.is_superuser |
||||||
|
|
@ -1,19 +1,29 @@ |
|||||||
from rest_framework import serializers |
from rest_framework import serializers |
||||||
from article.models import Article |
from article.models import Article |
||||||
|
from user_info.serializers import UserDescSerializer |
||||||
|
|
||||||
|
|
||||||
# 返回文章列表或创建一篇文章 |
# 返回文章列表或创建一篇文章 |
||||||
class ArticleListSerializer(serializers.ModelSerializer): |
class ArticleListSerializer(serializers.ModelSerializer): |
||||||
|
author = UserDescSerializer(read_only=True) |
||||||
|
# 使用article的url.py中的view --> detail |
||||||
|
url = serializers.HyperlinkedIdentityField(view_name="article:detail") |
||||||
|
|
||||||
class Meta: |
class Meta: |
||||||
model = Article |
model = Article |
||||||
fields = [ |
fields = [ |
||||||
'id', |
'url', |
||||||
'title', |
'title', |
||||||
'created', |
'created', |
||||||
'body', |
'body', |
||||||
|
'author', |
||||||
] |
] |
||||||
|
# read_only_fields = ['author'] |
||||||
|
|
||||||
|
# 返回文章详情 |
||||||
|
|
||||||
|
|
||||||
# 返回文章详情 |
|
||||||
class ArticleDetailSerializer(serializers.ModelSerializer): |
class ArticleDetailSerializer(serializers.ModelSerializer): |
||||||
class Meta: |
class Meta: |
||||||
model = Article |
model = Article |
||||||
fields = '__all__' |
fields = '__all__' |
Binary file not shown.
@ -0,0 +1,3 @@ |
|||||||
|
from django.contrib import admin |
||||||
|
|
||||||
|
# Register your models here. |
@ -0,0 +1,5 @@ |
|||||||
|
from django.apps import AppConfig |
||||||
|
|
||||||
|
|
||||||
|
class UserInfoConfig(AppConfig): |
||||||
|
name = 'user_info' |
@ -0,0 +1,3 @@ |
|||||||
|
from django.db import models |
||||||
|
|
||||||
|
# Create your models here. |
@ -0,0 +1,15 @@ |
|||||||
|
from django.contrib.auth.models import User |
||||||
|
from rest_framework import serializers |
||||||
|
|
||||||
|
|
||||||
|
class UserDescSerializer(serializers.ModelSerializer): |
||||||
|
"""文章列表中引用的嵌套序列化器""" |
||||||
|
|
||||||
|
class Meta: |
||||||
|
model = User |
||||||
|
fields = [ |
||||||
|
'id', |
||||||
|
'username', |
||||||
|
# 'last_login', |
||||||
|
# 'date_joined' |
||||||
|
] |
@ -0,0 +1,3 @@ |
|||||||
|
from django.test import TestCase |
||||||
|
|
||||||
|
# Create your tests here. |
@ -0,0 +1,3 @@ |
|||||||
|
from django.shortcuts import render |
||||||
|
|
||||||
|
# Create your views here. |
Loading…
Reference in new issue