【发布时间】:2018-02-23 02:36:35
【问题描述】:
我使用 Django 作为后端(Django Rest Framework),使用 Angular 作为前端。在后端,我有 2 个模型:
class Post(models.Model):
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
content = models.TextField(max_length=1000)
class Category(models.Model):
title = models.CharField(max_length=120)
我相应地序列化:
class CategorySerializer(ModelSerializer):
class Meta:
model = Category
fields = '__all__'
class PostSerializer(ModelSerializer):
class Meta:
model = Post
fields = '__all__'
然后在组件模板中我要打印特定帖子的类别标题:
<li *ngFor='let post of posts'>
<div *ngIf='post'>
<p>{{ post.category.title }}</p>
<p>{{ post.content }}</p>
</div>
</li>
我的课程是:
export class Post {
id: number;
category: Category;
content: string;
}
export class Category {
id: number;
title: string;
}
为什么{{ post.category.title }} 不起作用,而{{ post.content }} 起作用?这是我的序列化问题还是我的 Angular 类的问题?
编辑:API 视图:
class PostListCreateAPIView(ListCreateAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer
【问题讨论】:
标签: django angular django-rest-framework