【问题标题】:can't save model instance in pipeline method of django-social-auth无法在 django-social-auth 的管道方法中保存模型实例
【发布时间】:2013-06-17 09:45:22
【问题描述】:

今天我发现了一些奇怪的东西,我需要一个解释。

这是我当前的django-social-auth 管道

SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    'social_auth.backends.pipeline.associate.associate_by_email',
    'social_auth.backends.pipeline.user.get_username',
    'social_auth.backends.pipeline.user.create_user',
    'social_auth.backends.pipeline.social.associate_user',
    'apps.main.pipeline.load_user_data',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details'
)

如您所见,我有一个部分管道,即 load_user_data 方法,问题所在:

def load_user_data(request, user, *args, **kwargs):
    x = User.objects.get(id=user.id)
    x.fid = 123
    x.save() # <-- Nothing changes in db . I even used force_update attribute.
    
    user.fid = 123
    user.save() # <-- it works.

所以,我不明白为什么x.save() 不起作用,我为这个问题苦苦挣扎了好几个小时。

【问题讨论】:

    标签: python django django-socialauth


    【解决方案1】:
    def load_user_data(request, user, *args, **kwargs):
        x = User.objects.get_or_create(id=user.id) # this is looking for specyfic User, if nothing found it will create one. In your solution x wasnt User instance, so you cant save it.
        x.fid = 123
        x.save() # <-- Nothing changes in db . I even used force_update attribute.
    
        user.fid = 123 # User instance.
        user.save() # <-- it works.
    

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 2018-09-26
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 2020-06-19
      相关资源
      最近更新 更多