[英]In django, why does this call to save() clears data from parent table?
I am starting out with django and have gone through only beginners' documentation. 我从django开始,仅阅读了初学者的文档。 I was trying out the following. 我正在尝试以下方法。
models.py models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from org.models import Organization
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
organization = models.ForeignKey(Organization, null=True)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
When I create a user, the code works fine. 创建用户时,代码可以正常工作。 Row gets added to auth_user table and to 'profile' table. 将行添加到auth_user表和“配置文件”表中。 At the time of user creation I do not associate Organization to user. 在创建用户时,我没有将组织与用户相关联。 organization_id in the 'profile' table remains NULL. “配置文件”表中的organization_id保持为NULL。
mysql> select * from user_profile;
+----+---------------+---------+
| id | organization_id | user_id |
+----+---------------+---------+
| 2 | NULL | 3 |
+----+---------------+---------+
Later I associate user to organization. 后来我将用户与组织相关联。 I have the following code. 我有以下代码。
views.py views.py
...
def user_association_done(request):
organization_id = request.POST['organization_id']
user_id = request.POST['user_id']
organization = Organization(id=organization_id)
user = User(id=user_id)
user.profile.organization = organization
user.save()
return HttpResponse('Done')
...
The code above associates the organization to user (entry in the profile table gets updated to populate the organization_id.), 上面的代码将组织与用户相关联(配置文件表中的条目会更新以填充organization_id。),
mysql> select * from user_profile;
+----+---------------+---------+
| id | organization_id | user_id |
+----+---------------+---------+
| 2 | 1 | 3 |
+----+---------------+---------+
but the data from the row in auth_user table gets blanked out 但是auth_user表中的行中的数据被清空
mysql> select id, password, username from auth_user where id=3;
+----+----------+----------+
| id | password | username |
+----+----------+----------+
| 3 | | |
+----+----------+----------+
1 row in set (0.00 sec)
The row itself remains though. 该行本身仍然存在。 Obviously, I am not doing this right and perhaps the call to save() is not the right one. 显然,我没有做到这一点,也许对save()的调用不是正确的。
Instead of User(id=user_id)
and Organization(id=organization_id)
, do 代替User(id=user_id)
和Organization(id=organization_id)
,执行
User.objects.get(id=user_id)
Organization.objects.get(id=organization_id)
Try this function,
def user_association_done(request):
organization_id = request.POST['organization_id']
user_id = request.POST['user_id']
organization = Organization.objects.get(id=organization_id)
user = user.objects.create(id=user_id,organization=organization)
user.save()
return HttpResponse('Done')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.