在夹层项目上创建帐户时返回多个对象(配置文件)

时间:2013-04-05 20:10:09

标签: python django profiles

我在当地环境中设置了夹层(使用pybb)。 pybb有一个抽象的Profile类来定义一些用户属性,并且如果你还没有使用它,还会附带一个Profile类。

除了最终创建用户的注册过程外,一切正常,但导致以下追溯:

http://pastebin.com/MFuBpc2A

我认为pybbMiddleware可能是个问题。 (我如何创建2个配置文件?)

class PybbMiddleware(object):
     def process_request(self, request):
         if request.user.is_authenticated():
             try:
                # Here we try to load profile, but can get error
                # if user created during syncdb but profile model
                # under south control. (Like pybb.Profile).
                profile = request.user.get_profile()
            except ObjectDoesNotExist:
                # Ok, we should create new profile for this user
                # and grant permissions for add posts
                user_saved(request.user, created=True)
                profile = request.user.get_profile()

            language = translation.get_language_from_request(request)

            if not profile.language:
                profile.language = language
                profile.save()

            if profile.language and profile.language != language:
                request.session['django_language'] = profile.language
                translation.activate(profile.language)
                request.LANGUAGE_CODE = translation.get_language()

pybb的signals.py中的user_saved():

def user_saved(instance, created, **kwargs):
    if not created:
        return
    try:
        add_post_permission = Permission.objects.get_by_natural_key('add_post', 'pybb', 'post')
        add_topic_permission = Permission.objects.get_by_natural_key('add_topic', 'pybb', 'topic')
    except ObjectDoesNotExist:
        return
    instance.user_permissions.add(add_post_permission, add_topic_permission)
    instance.save()
    if settings.AUTH_PROFILE_MODULE == 'pybb.Profile':
        from models import Profile
        Profile(user=instance).save()

最重要的是我在accounts / models.py中有另一个user_saved():

@receiver(post_save, sender=User)
def user_saved(sender=None, instance=None, **kwargs):
    Profile.objects.get_or_create(**{str(user_field): instance})

它是否同时调用两个配置文件?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我怀疑您正在使用mezzanine.accounts并在settings.py中设置了AUTH_PROFILE_MODULE。 mezzanine.accounts.models中的代码设置一个信号,以便在保存用户模型时生成配置文件。在使用tastypie创建用户和配置文件时,我实际上遇到了同样的问题。