models.py
var direction = "left";
$('#elem').css("margin-"+direction, "50px");
我尝试了两种方法,但都失败了,请指导我。谢谢
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)
第二种方法很奇怪
更新后# 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
和areapoint.x_axis
确实有所不同。
但在我的数据库中。它仍然是一样的。
答案 0 :(得分:7)
在这两种方法中,您都试图更新Area
对象而不是AreaPoint
对象。
以下是使用这两种方法的方法:
第一种方法:使用update
方法:
# 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)
上面会返回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
# save obj.areapoint instead
obj.areapoint.save()
第3种方法:
areapoint = AreaPoint.objects.get(area_id=304)
areapoint.x_axis = 100
areapoint.y_axis = 200
areapoint.save()