问题描述
models.py
class Area(models.Model):
area_name = models.CharField(max_length = 255,null = False,blank = False)
描述= models.TextField(null = False,blank = False)
class AreaPoint(models.Model):
x_axis = models.FloatField(default = 0.0)
y_axis = models.FloatField(default = 0.0)
area = models.OneToOneField(Area,primary_key = True,on_delete = models.CASCADE)
我尝试了两种方法,但是都失败了,请指导我。谢谢
#第一种方法:
#Area.objects.filter(id = 304).update(area_name = 今天是1,description =今天是1,areapoint__x_axis = 111,areapoint__y_axis = 222)
#错误:区域没有名为'areapoint__y_axis'的字段
#第二种方法:
obj = Area.objects.get(id = 304)
打印obj.areapoint.x_axis#277
打印obj.areapoint.y_axis#65
obj.areapoint.x_axis = 100
obj.areapoint.y_axis = 200
打印obj.areapoint.x_axis#100
打印obj.areapoint.y_axis#200
obj.save()
打印obj .areapoint.x_axis#100
打印obj.areapoint.y_axis#200
第二种方法
areapoint.x_axis
和 areapoint.y_axis
在更新后确实有所不同。
但是在我的数据库中。
在两种方法中,您都尝试更新区域
对象,而不是 AreaPoint
对象。
以下是两种方法的实现方法:
第一种方法:使用更新
方法:
#这是您的工作:
Area.objects.filter(id = 304).update(area_name = today is 1,
说明=今天是1,
areapoint__x_axis = 111,
areapoint__y_axis = 222)
以上将返回对象 Area
,并且由于没有字段 areapoint__x_axis
等,因此会引发错误。
您可以做的是在 AreaPoint
上进行过滤并更新:
AreaPoint.objects.filter(area_id = 304).update(x_axis = 111,y_axis = 222)
第二种方法:
obj = Area.objects.get(id = 304)
obj.areapoint.x_axis = 100
obj.areapoint.y_axis = 200
#保存obj.areapoint代替
obj.areapoint.save()
第三种方法:
areapoint = AreaPoint.objects.get(area_id = 304)
areapoint.x_axis = 100
areapoint.y_axis = 200
areapoint.save()
models.py
class Area(models.Model):
area_name = models.CharField(max_length=255, null=False, blank=False)
description = models.TextField(null=False, blank=False)
class AreaPoint(models.Model):
x_axis = models.FloatField(default=0.0)
y_axis = models.FloatField(default=0.0)
area = models.OneToOneField(Area,primary_key=True,on_delete=models.CASCADE)
I try two method , but both fail ,please guide me. thank you
# first method :
# Area.objects.filter(id=304).update(area_name="today is 1", description="today is 1", areapoint__x_axis=111,areapoint__y_axis=222)
# error : Area has no field named 'areapoint__y_axis'
# second method :
obj = Area.objects.get(id=304)
print obj.areapoint.x_axis # 277
print obj.areapoint.y_axis # 65
obj.areapoint.x_axis = 100
obj.areapoint.y_axis = 200
print obj.areapoint.x_axis # 100
print obj.areapoint.y_axis # 200
obj.save()
print obj.areapoint.x_axis # 100
print obj.areapoint.y_axis # 200
The second method is weird.
areapoint.x_axis
and areapoint.y_axis
are really different after update. But in my database.It still the same .
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()
这篇关于django-orm:如何更新一对一的关系字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!