如何为django中的一对一字段赋值
How to assign value to one-to-one field in django
我刚刚开始学习 Django 并尝试使用 manage.py shell 为我的模型中的一对一字段赋值。我尝试这样做,但不确定为什么不将值分配给 Author.address
author1 = Author.objects.get(first_name="Sam")
addr1 = Address.objects.get(post_code="12345")
author1.address = addr1
我错过了什么步骤吗?
class Author(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
address = models.OneToOneField(Address, on_delete=models.CASCADE, null=True)
def full_name(self):
return f"{self.first_name} {self.last_name}"
def __str__(self):
return self.full_name()
class Address(models.Model):
street = models.CharField(max_length=80)
postal_code = models.CharField(max_length=5)
city = models.CharField(max_length=50)
def __str__(self):
return f"{self.street}"
谢谢
您应该在分配后保存模型
author1 = Author.objects.get(first_name="Sam")
addr1 = Address.objects.get(post_code="12345")
author1.address = addr1
author1.save()
您还可以使用 update 方法更新一对一关系
addr1 = Address.objects.get(post_code="12345")
Author.objects.filter(first_name="Sam").update(address=addr1)
3种方法可用
这是例子。
found = User.objects.get(email=obj.email) if not obj.email is None else None
if found and obj.user is None:
#method 1found = User.objects.get(email=obj.email) if not obj.email is None else None
if found and obj.user is None:
#method 1
Profile.objects.filter(pk=obj.id).update(user=found)
#method 2
obj.user = found
obj.save(update_fields=['user'])
#method 3
with connection.cursor() as cursor:
cursor.execute("UPDATE users_profile SET user_id = %s WHERE id = %s", ( found.id, obj.id ) )
我刚刚开始学习 Django 并尝试使用 manage.py shell 为我的模型中的一对一字段赋值。我尝试这样做,但不确定为什么不将值分配给 Author.address
author1 = Author.objects.get(first_name="Sam")
addr1 = Address.objects.get(post_code="12345")
author1.address = addr1
我错过了什么步骤吗?
class Author(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
address = models.OneToOneField(Address, on_delete=models.CASCADE, null=True)
def full_name(self):
return f"{self.first_name} {self.last_name}"
def __str__(self):
return self.full_name()
class Address(models.Model):
street = models.CharField(max_length=80)
postal_code = models.CharField(max_length=5)
city = models.CharField(max_length=50)
def __str__(self):
return f"{self.street}"
谢谢
您应该在分配后保存模型
author1 = Author.objects.get(first_name="Sam")
addr1 = Address.objects.get(post_code="12345")
author1.address = addr1
author1.save()
您还可以使用 update 方法更新一对一关系
addr1 = Address.objects.get(post_code="12345")
Author.objects.filter(first_name="Sam").update(address=addr1)
3种方法可用 这是例子。
found = User.objects.get(email=obj.email) if not obj.email is None else None
if found and obj.user is None:
#method 1found = User.objects.get(email=obj.email) if not obj.email is None else None
if found and obj.user is None:
#method 1
Profile.objects.filter(pk=obj.id).update(user=found)
#method 2
obj.user = found
obj.save(update_fields=['user'])
#method 3
with connection.cursor() as cursor:
cursor.execute("UPDATE users_profile SET user_id = %s WHERE id = %s", ( found.id, obj.id ) )