Saving profile with registration in Django-Registration

前端 未结 3 795
名媛妹妹
名媛妹妹 2020-12-23 23:36

In Django-Registration it says you can save a custom profile when you save a user.
But I have no idea what the documentation is asking me to do. Here is what they say

3条回答
  •  春和景丽
    2020-12-23 23:49

    This is covered in this blogpost and expanded on in my answer to another question on the same issue

    django-registration sends a signal at various events happening - registration and activation. At either of those points you can create a hook to that signal which will be given the user and request objects - from there you can create a profile for that user.

    The signal from django-registration

    #registration.signals.py 
    user_registered = Signal(providing_args=["user", "request"]) 
    

    Code to create profile

    #signals.py (in your project)
    user_registered.connect(create_profile)
    
    def create_profile(sender, instance, request, **kwargs):
        from myapp.models import Profile
        #If you want to set any values (perhaps passed via request) 
        #you can do that here
    
        Profile(user = instance).save()
    

提交回复
热议问题