django-orm : How to update one-to-one relation field value

与世无争的帅哥 提交于 2019-12-07 02:31:24
AKS

In both the approach, you are trying to update the Area object and not the AreaPoint object.

Here is how you could do it using both approaches:

1st Approach: using update method:

# here is what you are doing:
Area.objects.filter(id=304).update(area_name="today is 1",
                                   description="today is 1",
                                   areapoint__x_axis=111,
                                   areapoint__y_axis=222)

Above will return an object of Area and since there are no fields areapoint__x_axis etc. it throws error.

What you could do is filter on AreaPoint instead and update it:

AreaPoint.objects.filter(area_id=304).update(x_axis=111, y_axis=222)

2nd Approach:

obj = Area.objects.get(id=304)
obj.areapoint.x_axis = 100
obj.areapoint.y_axis = 200 

# save obj.areapoint instead
obj.areapoint.save()

3rd Approach:

areapoint = AreaPoint.objects.get(area_id=304)
areapoint.x_axis = 100
areapoint.y_axis = 200
areapoint.save()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!