我正在尝试通过Django REST框架在数据库中PATCH
使用一个对象。该请求在后端处理,更新后的属性(friendship_status
)是我收到的响应的一部分。但是:该属性未在数据库中更新。或者,至少它没有显示在Django Admin中。
我在这里想念什么?
请求
fetch(host + '/api/relationships/' + relationshipID + '/', {
method: 'PATCH',
headers: {
"Content-Type": "application/json",
"Authorization": "Token " + token,
},
body: JSON.stringify({
friendship_status: 'requested',
}),
})
// plus some .then() method calls
回复
headers:Headers
ok:true
status:200
statusText:undefined
type:"default"
url:"http://localhost:8000/api/relationships/27aae52c-020b-4d3d-8037-03ae005fb7ef/"
_bodyInit:"{"id":"27aae52c-020b-4d3d-8037-03ae005fb7ef","friendship_status":"requested","is_blocked":false,"created":"2018-10-22T09:53:50.550167","modified":"2018-10-22T10:35:16.657392","from_user":29,"to_user":28}"
_bodyText:"{"id":"27aae52c-020b-4d3d-8037-03ae005fb7ef","friendship_status":"requested","is_blocked":false,"created":"2018-10-22T09:53:50.550167","modified":"2018-10-22T10:35:16.657392","from_user":29,"to_user":28}"
__proto__:Object
DefaultRouter
router = DefaultRouter()
router.register(r'relationships', relationship_views.RelationshipViewSet)
ModelViewSet
class RelationshipViewSet(viewsets.ModelViewSet):
queryset = Relationship.objects.all()
serializer_class = RelationshipSerializer
def get_queryset(self):
return Relationship.objects.filter(from_user=self.request.user)
ModelSerializer
class RelationshipSerializer(serializers.ModelSerializer):
from_user = AccountSerializer(read_only=True)
to_user = AccountSerializer(read_only=True)
class Meta:
model = Relationship
fields = '__all__'
模型
class Relationship(models.Model):
FRIENDSHIP_STATUS_CHOICES = (
('ignored', 'Ignored'),
('requested', 'Requested'),
('accepted', 'Accepted'),
)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
from_user = models.ForeignKey('accounts.Account', on_delete=models.CASCADE, related_name='relationships')
to_user = models.ForeignKey('accounts.Account', on_delete=models.CASCADE, related_name='to_user')
friendship_status = models.CharField(choices=FRIENDSHIP_STATUS_CHOICES, max_length=20, null=True, blank=True)
is_blocked = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True, verbose_name=_('Created'))
modified = models.DateTimeField(auto_now=True, verbose_name=_('Last modified'))
admin.py
@admin.register(Relationship)
class RelationshipAdmin(ExcelExportMixin, admin.ModelAdmin):
list_display = ('id', 'from_user', 'to_user', 'friendship_status', 'is_blocked', 'created', 'modified')
search_fields = ('from_user', 'to_user')
答案 0 :(得分:0)
我通过重建Docker容器来解决该问题:docker-compose build
。