简体   繁体   English

在Django中,为什么调用save()从父表中清除数据?

[英]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()的调用不是正确的。

  1. What is the right way? 正确的方法是什么?
  2. I am curious as to why/how the data gets blanked out from the parent table(auth_user in this case) 我很好奇为什么/如何从父表中清空数据(在这种情况下为auth_user)

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.

相关问题 Django。 为什么我的外键与父主键中的相同数据不匹配 - Django. Why would my foreign key does not match the same data from parent primary key 为什么我的python脚本用来自另一个表的伪数据填充Django表会导致错误? - Why does my python script to populate a Django table with dummy data from another table result in error? 在数据库层次结构中从Django中的父表中获取数据 - Fetching data from parent table in Django in database hierarchy 使用表单和表单集创建视图以将数据保存在 django m2m 父表和中间(通过)表中 - Create view to save data in django m2m parent and intermediate (through) table using a form and formset 为什么Django post_save信号给我pre_save数据? - Why does Django post_save signal give me pre_save data? 为什么Django ModelForm有时在save()调用之前更改其内部实例对象? - Why does Django ModelForm change it's internal instance object before save() call.. sometimes? Django为什么两次保存图片? - Why does Django save pictures twice? 为什么django model保存function没有反应? - Why django model save function does not respond? Django视图结合父子表数据 - Django view to combine parent and child table data 在 django 上动态创建表并将数据保存在数据库中 - Create Dynamically table and save data in database on django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM