tlu*_*nga 1 django django-models
我找不到从 Django 模型中的 B 类更新 A 类中“no”值的方法
class A(models.Model):
no = models.IntegerField()
class B(models.Model):
a = models.ForeignKey(A)
def UpdateNo(self)
#update no of class A
Run Code Online (Sandbox Code Playgroud)
如何更新“否”字段
您可以使用 获取对象self.a,然后更改并保存该对象,例如:
class A(models.Model):
no = models.IntegerField()
class B(models.Model):
a = models.ForeignKey(A)
def UpdateNo(self):
my_a = self.a # fetch the related A object in my_a
my_a.no = 1234 # update the no field of that object
my_a.save() # save the update to the databaseRun Code Online (Sandbox Code Playgroud)
AForeignKey将获取延迟相关的对象,因此self.a将包含相关的A对象。因此,我们可以像处理任何模型对象一样处理它。
| 归档时间: |
|
| 查看次数: |
2009 次 |
| 最近记录: |