我的数据是这样的,我想序列化它们而不为它们创建模型。
[
{'form': 1, 'count': 1},
{'form': 2, 'count': 3}
]
Run Code Online (Sandbox Code Playgroud)
序列化到
[
{'form': 'my form name 1', 'count': 1},
{'form': 'my form name 2', 'count': 3}
]
Run Code Online (Sandbox Code Playgroud)
我想用序列化器序列化它,通过 pk 获取表单名称表单模型。
[
{'form': 1, 'count': 1},
{'form': 2, 'count': 3}
]
Run Code Online (Sandbox Code Playgroud)
一些错误,例如'int' object has no attribute 'pk'
在python控件中测试
[
{'form': 'my form name 1', 'count': 1},
{'form': 'my form name 2', 'count': 3}
]
Run Code Online (Sandbox Code Playgroud) 模型.py
class Form(models.Model):
no = models.IntegerField()
finish_date = models.DateField(blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
序列化程序.py
class FormSerializer(serializers.ModelSerializer):
class Meta:
model = Form
fields = '__all__'
Run Code Online (Sandbox Code Playgroud)
如果我尝试:
http http://127.0.0.1:8000/api/forms no=112 "finish_date"=""
Run Code Online (Sandbox Code Playgroud)
它返回错误:
"finish_date": [
"Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]."
]
Run Code Online (Sandbox Code Playgroud)
如果我将 "finish_date" 设置为 null ,则这篇文章有效。并且StringField(blank=True, null=True)
不会得到错误。
怎么解决?
http://127.0.0.1:8000/app_restFramework/users/,返回文字
/ app_restFramework/users /'User'对象的AttributeError没有属性'books'
models.py
class User(models.Model):
username = models.CharField(max_length=100)
class Book(models.Model):
name = models.CharField(max_length=100)
author = models.CharField(max_length=100)
publisher = models.CharField(max_length=100)
time = models.CharField(max_length=100)
owner = models.ManyToManyField(User)
Run Code Online (Sandbox Code Playgroud)
serializers.py
from app_restFramework.models import Book,User
class UserSerializer(serializers.ModelSerializer):
books = serializers.PrimaryKeyRelatedField(many = True, read_only = True)
class Meta:
model = User
fields = ('id', 'username', 'books')
Run Code Online (Sandbox Code Playgroud)
views.py
class UserList(generics.ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
Run Code Online (Sandbox Code Playgroud)
urls.py
url(r'^app_restFramework/users/$', app_restFramework.views.UserList.as_view() ),
Run Code Online (Sandbox Code Playgroud) 月-detail.component.html
import ...
@Component({
template: `{{month?.id}} <app-month-date [month]="month"></app-month-date>`
})
export class MonthDetailComponent implements OnInit {
month: Month;
ngOnInit(): void {
this.route.params
.switchMap((params: Params) => this.monthService.getMonth(+params["id"]))
.subscribe(month => (this.month = month));
}
}
Run Code Online (Sandbox Code Playgroud)
月-日.component.html
<p>month-date works! {{month?.id}}</p>
Run Code Online (Sandbox Code Playgroud)
月-日.component.ts
import { Component, OnInit, Input } from "@angular/core";
import { Month } from "app/attendance/month";
@Component({
selector: "app-month-date",
...
})
export class MonthDateComponent implements OnInit {
@Input() month: Month;
ngOnInit() {
//undefined --- here
console.log(this.month);
}}
Run Code Online (Sandbox Code Playgroud)
month?.id 在 中正确显示month-detail.component.html
,但app-month-date
在 中带有标记的月份未定义 …
我尝试用接口解构赋值,但不能这样写。
interface TYPE {
id?: number;
type?: string;
}
const e = {
'id': 123,
'type': 'type_x',
'other': 'other_x'
}
const {...foo}: {foo: TYPE} = e;
console.log(foo.id, foo.type) // expected: 123, 'type_x'
Run Code Online (Sandbox Code Playgroud) [a,b] = [b, a+b]
在这里不起作用,ab始终为0和1。
如果使用临时变量交换值,则可以。
function fibonacciSequence() {
let [a, b, arr] = [0, 1, []]
while (a <= 255) {
arr.concat(a)
[a, b] = [b, a + b]
console.log(a, b) // always 0 1
}
}
console.log(fibonacciSequence())
Run Code Online (Sandbox Code Playgroud)