我在本地环境中有一个工作夹层(带有 pybb)设置。pybb 有一个抽象的 Profile 类来定义一些用户属性,并且还提供了一个 Profile 类,以防你还没有使用。
一切正常,除了最终创建用户的注册过程,但导致以下回溯:
我认为 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})
它是否同时调用两者并因此创建 2 个配置文件?
谢谢你的帮助!