我需要在一个请求categories中多次更新Article。
在ArticleViewSet我有:
def get_serializer_class(self):
if self.action in ['partial_update', 'update']:
return ArticlePostSerializer
return ArticleSerializer
Run Code Online (Sandbox Code Playgroud)
所以ArticlePostSerializer需要改变。
这是我的序列化程序代码:
class ArticleShortCategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = 'id', 'name'
class ArticleSerializer(serializers.ModelSerializer):
categories = serializers.SerializerMethodField()
def get_categories(self, obj):
return ArticleShortCategorySerializer(obj.categories, many=True).data
class Meta:
model = Article
read_only_fields = 'id'
fields = ('categories', 'text') + read_only_fields
class ArticlePostSerializer(serializers.ModelSerializer):
class Meta:
model = Article
fields = 'id', 'categories', 'text'
Run Code Online (Sandbox Code Playgroud)
我尝试添加:
class ArticlePostListSerializer(serializers.ListSerializer):
Run Code Online (Sandbox Code Playgroud)
和
class Meta:
list_serializer_class = ArticlePostListSerializer …Run Code Online (Sandbox Code Playgroud) 如何在多选中设置默认选定值。我从数据库中获取current_options和all_options我想更新current_options并再次发送新值做数据库。
更新数据库有效,但当我刷新页面时,没有选择任何选项。
current_options = [{id:1, name:'name1'}]; #from database
all_options = [{id:1, name:'name1'},{id:2, name:'name2'}]; #from database
Run Code Online (Sandbox Code Playgroud)
我的模板:
<select multiple name="type" [(ngModel)]="current_options">
<option *ngFor="let option of all_options" [ngValue] = "option">
{{option.name}}
</option>
</select>`
Run Code Online (Sandbox Code Playgroud) 在下面的示例中,如何obj.category转换成Category文字?
我需要它来设置下拉菜单中的选定选项。
export class Category{
id: number;
name: string;
constructor(obj: any) {
this.id = obj.id;
this.name = obj.name;
}
}
export class Post {
category: Category[];
constructor(obj: any) {
this.category = obj.category;
}
}
Run Code Online (Sandbox Code Playgroud)
类别服务如下所示:
getCategories(): Promise<Category[]> {
return this.http.get(this.appConfig.apiEndpoint + 'categories/').toPromise()
.then(response => response.json().map(obj => new Category(obj)))
.catch(this.handleError);
}
Run Code Online (Sandbox Code Playgroud)
api回应:
[{"id":1,"name":"cat1"},{"id":2,"name":"cat2"}]
Run Code Online (Sandbox Code Playgroud)
模板:
<select multiple name="category" [(ngModel)]="obj.post.category">
<option *ngFor="let category of categories" [ngValue] = "category">
{{category.name}}
</option>
</select>
Run Code Online (Sandbox Code Playgroud) 我需要修改cordova-plugin-media,但是当我运行应用程序时ionic cordova run android不会应用我的更改。
我应该如何正确修改和运行插件?