作者:榜榜爱打球 | 来源:互联网 | 2024-10-13 20:09
我是Django的初学者。现在,我正在使用API。我面临一个问题。我无法在http://127.0.0.1:8000/gameapi/上查看名为label
的字段之一。这是屏幕截图:
这是我在
gamreview 文件夹中的
serializers.py
代码。
from rest_framework import serializers
from .models import Game,Tags
class TagSerializer(serializers.ModelSerializer):
class Meta:
model = Tags
fields = ['label']
class GameSerializer(serializers.ModelSerializer):
# label_tag = TagSerializer(many=True)
class Meta:
model = Game
fields = ['id','title','developer','platform']
fields = ['id','platform','label_tag']
def create(self,validated_data):
label_tag_data = validated_data.pop('label_tag')
game = Game.objects.create(**validated_data)
for tags_data in label_tag_data:
Tags.objects.create(game=game,**tags_data)
return Game.objects.create(**validated_data)
def update(self,instance,validated_data):
instance.title = validated_data.get('title',instance.title)
instance.developer = validated_data.get('developer',instance.developer)
instance.platform = validated_data.get('platform',instance.platform)
instance.tag = TagSerializer(read_Only=True,many=True)
instance.save()
return instance
这是我在 gamreview 文件夹下的models.py
的代码:
from django.db import models
from django.template.defaultfilters import slugify
# Create your models here.
class Tags(models.Model):
label = models.CharField(max_length=20)
def __str__(self):
return self.label
class Game(models.Model):
title = models.CharField(max_length=100)
developer = models.CharField(max_length=100)
platform = models.CharField(max_length=50,default='null')
label_tag = models.ManyToManyField(Tags)
slug = models.SlugField(max_length=150,default='null')
def __str__(self):
return self.title
def save(self,*args,**kwargs):
self.slug = slugify(self.title)
super().save(*args,**kwargs)
class Review(models.Model):
game = models.ForeignKey(Game,on_delete=models.CASCADE)
review = models.CharField(max_length=1000)
date = models.DateField(auto_now=True)
slug = models.SlugField(max_length=150,default='null')
def __str__(self):
return self.game
这是我在 gamreview 文件夹下的views.py
的代码:
from django.views import generic
from .models import Game
from rest_framework import generics
from .serializers import GameSerializer
# Create your views here.
class GameListView(generic.ListView):
template_name = 'gamereview/gamelist.html'
context_object_name = 'all_games'
def get_queryset(self):
return Game.objects.all()
class ReviewView(generic.DetailView):
model = Game
template_name = 'gamereview/review.html'
# class GameApiView(generics.ListAPIView):
class GameApiView(generics.ListCreateAPIView):
queryset = Game.objects.all()
serializer_class = GameSerializer
class GameDetailApiView(generics.RetrieveUpdateDestroyAPIView):
queryset = Game.objects.all()
serializer_class = GameSerializer
这是我在 gamreview 文件夹下的urls.py
的代码:
from . import views
from django.urls import path
app_name = 'gamereview'
urlpatterns = [
path('gamereview/',views.GameListView.as_view(),name='gamelist'),path('gamereview//',views.ReviewView.as_view(),name='review'),path('gameapi/',views.GameApiView.as_view(),name='gamelistapi'),path('gameapi//',views.GameDetailApiView.as_view()),]
运行服务器时没有任何错误。但是,label
字段没有显示。
我该如何解决该问题?
您在fields
-GameSerializer
类中两次声明了Meta
。删除第一个。
class GameSerializer(serializers.ModelSerializer):
# label_tag = TagSerializer(many=True)
class Meta:
model = Game
fields = ['id','title','developer','platform'] --> delete this
fields = ['id','platform','label_tag']
,
Django rest框架依赖于related_name
属性来解析外键字段。
我认为只需更改游戏模型即可
label_tag = models.ManyToManyField(Tags,related_name="label")