我有以下简单的models.py
文件:
from django.db import models
from model_utils.managers import InheritanceManager
class Clique(models.Model):
created = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=100, blank=False)
class Post(models.Model):
created = models.DateTimeField(auto_now_add=True)
headline = models.TextField()
clique = models.ForeignKey(Clique,
on_delete=models.CASCADE,
blank=True,
null=True)
objects = InheritanceManager()
def __str__(self):
return self.headline
class VideoPost(Post):
video = models.BooleanField(default=True)
class ImagePost(Post):
image = models.BooleanField(default=True)
因此,有一个Clique
模型可以包含多个Post
实例。 Post
实例可以是ImagePost
或VideoPost
。因此,ImagePost
和VideoPost
都继承Post
。
现在,假设我要检索ImagePost子类实例。因此,我的views.py文件中具有以下视图:
class PostList(generics.ListAPIView):
serializer_class = PostSerializer
def get_queryset(self):
return Post.objects.select_subclasses(ImagePost)
当我在URL中传递端点posts/
时,将触发此视图,并且该视图应该仅给我ImagePost
实例,对吗?但是我也从数据库中获得了VideoPost
实例:
[
{
"clique": "http://127.0.0.1:8000/cliques/1/",
"comment_set": [],
"created": "2019-06-18T09:52:47.929623Z",
"headline": "FirstImagePost",
"id": 1,
"url": "http://127.0.0.1:8000/posts/1/"
},
{
"clique": "http://127.0.0.1:8000/cliques/1/",
"comment_set": [],
"created": "2019-06-18T09:53:20.266653Z",
"headline": "FirstVideoPost",
"id": 2,
"url": "http://127.0.0.1:8000/posts/2/"
}
]
为什么会这样?我浏览了官方文档here。有人可以帮忙吗
仅出于完整性考虑,我的serializers.py
文件如下所示:
from rest_framework import serializers
from posts.models import Post, VideoPost, ImagePost, Clique
class CliqueSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Clique
fields = ('id', 'name', 'post_set')
read_only_fields = ('post_set', )
class PostSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Post
fields = ('url', 'id', 'created', 'headline', 'clique', 'comment_set',)
read_only_fields = ('comment_set',)
class VideoPostSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = VideoPost
fields = '__all__'
class ImagePostSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ImagePost
fields = '__all__'
答案 0 :(得分:1)
从文档中看来,#! /bin/bash
names=(Donald Alan Brian)
selected=()
PS3='Name? You can select multiple space separated options: '
select name in "${names[@]}" ; do
for reply in $REPLY ; do
selected+=(${names[reply - 1]})
done
[[ $selected ]] && break
done
echo Selected: "${selected[@]}"
并没有按照您的子类类型进行过滤,只有在与您提供的内容相匹配时,才将其转换为子类。
以您的情况
select_subclasses
会将所有ImagePost转换为ImagePost实例,将其他实例保留为Post对象,但不会将其过滤掉。
来自此处的文档:
Post.objects.select_subclasses(ImagePost)
就您而言,您可以简单地做到:
nearby_places = Place.objects.select_subclasses("restaurant")
# restaurants will be Restaurant instances, bars will still be Place instances
尽管我认为您不需要Post.objects.filter(imagepost__image=True).select_subclasses(ImagePost)
部分