我试图添加评论到我的数据库,但收到错误 'OrderedDict' object has no attribute 'pk'
react.js处理POST请求的代码部分:
addComment() {
let url = this.props.post_url
axios.post('/api/comments/', {
post: url,
user: "http://127.0.0.1:8000/api/users/1/?format=json",
text: document.getElementsByName(url)[0].value,
csrfmiddlewaretoken: document.getElementsByName("csrfmiddlewaretoken")[0].value},
)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
Run Code Online (Sandbox Code Playgroud)
我的serializers.py:
from django.contrib.auth.models import User
from rest_framework import serializers
from .models import Post, Comment
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'url')
class CommentSerializer(serializers.HyperlinkedModelSerializer):
#user = UserSerializer(many=False, required=False)
class Meta:
model = Comment
fields = ('id', "post", …Run Code Online (Sandbox Code Playgroud) 直到今天我还以为 Java 中的 ArrayList 和 C++ std::vectors 是一样的,但事实证明 ArrayList 只能存储对象。(原语被包裹)。我正在寻找的是这样的:
class Array {
int i;
type[] array;
Array() {
this.i = 0;
this.array = new array[1];
}
Array(int initialSize) {
this.i = 0;
this.array = new array[initialSize];
}
type get(int index) {
return array[index];
}
void add(type to_add) {
if(array.length == i) {
type[] newArray = new type[array.length * 2];
for(int j = 0; j < array.length; j++) newArray[j] = array[j];
}
array[i++] = to_add;
}
}
Run Code Online (Sandbox Code Playgroud)
“type”是原始数据类型之一。
注意:我不需要存储对象的能力,所以我不想要 ArrayList …