我有这样的模型:
class Category(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(unique=True, max_length=255, blank=True,default=None)
desc = models.TextField(blank=True, null=True )
...
class Post(models.Model):
title = models.CharField(max_length=255)
pub_date = models.DateTimeField(editable=False,blank=True)
author = models.ForeignKey(User, null=True, blank=True)
categories = models.ManyToManyField(Category, blank=True, through='CatToPost')
...
class CatToPost(models.Model):
post = models.ForeignKey(Post)
category = models.ForeignKey(Category)
...
这是序列化器:
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = ('name','slug')
class PostSerializer(serializers.ModelSerializer):
categories = CategorySerializer(many=True, required=True)
class Meta:
model = Post
.......
views.py
class SingleListing(generics.RetrieveUpdateDestroyAPIView):
queryset = Post.objects.all()
serializer_class =Post
串行
但是这不会在Web浏览视图中显示类别字段。我只看到类别标签,但此字段的输入字段不存在。问题是什么?
答案 0 :(得分:0)
DRF不支持many=True
并且Lists are not currently supported in HTML input.
Category
上升class PostSerializer(serializers.ModelSerializer):
categories = CategorySerializer(required=True)
class Meta:
model = Post
fields = ('id', 'title', 'pub_date', 'author', 'categories')
不是一个模型的字段。它将显示所有与模型相关的字段。
试试这个:
class SingleViewSet(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
private static double LatitudeDiffToMeters(double latitudeDiff)
{
return (R*latitudeDiff*Math.PI)/180;
}
private static double LongitudeDiffToMeters(double longitudeDiff, double latitude)
{
return (longitudeDiff*Math.PI* R * Math.Cos (Math.PI * latitude / 180))/180;
}
private static double CalculateLatitude(double latitude, double offset)
{
return latitude + ((offset/R) * (180/Math.PI));
}
private static double CalculateLongitude(double longitude, double latitude, double offset)
{
return longitude + offset / (R * Math.Cos (Math.PI * latitude / 180)) * (180 / Math.PI);
}
在web-browserview中显示字段
答案 1 :(得分:0)
好吧,你应该像这样编写序列化器:
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = ('name','slug')
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ('id','{anything you want}','categories')
depth = 2