我有一个像这样的字典:
profile_data={'trial_duration': 0,'tac_agreed': True,'email': 'blahblah@gmail.com','signup_method': 'google','social_id': '2343432432','social_platform': 'google','name': 'blahblah tester'}
我想使用update_or_create方法将此字典映射到两个不同的模型对象。但是,Django处理此问题的方式似乎有些矛盾。
我有一个社交信息模型:
class SocialInformation(models.Model):
social_id = models.CharField(max_length=256,blank=True,null=True)
social_platform = models.CharField(max_length=256,choices=SOCIAL_PLATFORM_CHOICES,default='',null=True)
当我使用以下行时,它会无例外地执行并且值将按预期进行映射,即使 profile_data 字典中的大多数项目也不是 SocialInformation 上的字段em>模型,则映射只会忽略不相关的映射,并采用相关的映射。
social_info,created = SocialInformation.objects.update_or_create(defaults={**profile_data})
但是,当我对不同的模型执行相同的操作时,配置文件:
class Profile(models.Model):
owner = models.OneToOneField('auth.User',primary_key=True,on_delete=models.CASCADE)
name = EncryptedCharField(max_length=200,null=True)
email = models.EmailField(blank=True)
trial_duration = models.IntegerField(default=7)
signup_method = models.CharField(max_length=256,choices=SINGUP_METHOD_CHOICES,blank=True)
etc. etc.
如果我执行同一行:
profile,created = Profile.objects.update_or_create(owner=user,defaults={**profile_data})
我收到以下错误:
FieldError: Invalid field name(s) for model Profile: 'social_id','social_platform'.
为什么它不像 SocialInformation 查询那样忽略 Profile 模型中没有的字段?