django-registration creating blank django-profiles using signals

試著忘記壹切 提交于 2019-12-13 07:36:12

问题


I have gone through the solution here and Shacker's django-profiles: The Missing Manual and subsequent signals solution by Dmitko and answers here and many places.

Here are the details:

AUTH_PROFILE_MODULE = "members.Profile"

From members.models.py

class Profile(models.Model):
    """member model."""
    GENDER_CHOICES = (
        (1, 'Male'),
        (2, 'Female'),
    )
    user = models.ForeignKey(User, unique=True)
    first_name = models.CharField(_('first name'), max_length=100)
    middle_name = models.CharField(_('middle name'), blank=True, max_length=100)
    last_name = models.CharField(_('last name'), max_length=100)
    slug = models.SlugField(_('slug'), unique=True)
    gender = models.PositiveSmallIntegerField(
        _('gender'), 
        choices=GENDER_CHOICES, 
        blank=True, 
        null=True,
        )
    birth_date = models.DateField(_('birth date'), blank=True, null=True)

    class Meta:
        verbose_name = _('Profiles')
        verbose_name_plural = _('Profiles')
        db_table = 'Profiles'
        ordering = ('last_name', 'first_name',)

From members.forms.py:

from django.contrib.auth.models import User
from basic.members.models import Profile
from registration.forms import RegistrationForm, RegistrationFormTermsOfService, RegistrationFormUniqueEmail
from django.db.models import signals
from basic.members.signals import create_profile

attrs_dict = { 'class': 'required' }

class ProfileForm(RegistrationFormTermsOfService, RegistrationFormUniqueEmail):
    GENDER_CHOICES = (
        (1, 'Male'),
        (2, 'Female'),
    )
    first_name = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))
    last_name = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))
    gender = forms.IntegerField(widget=forms.RadioSelect(choices=GENDER_CHOICES))
    birth_date = forms.DateField(widget=SelectDateWidget(years=range(1911,1999)))

    def save(self, user):
        try:    
            data = user.get_profile()
        except:
            data = Profile(user=user)
            data.first_name = self.cleaned_data["first_name"]
            data.first_name = self.cleaned_data["last_name"]
            data.save()

class UserRegistrationForm(RegistrationForm):
    first_name = forms.CharField(max_length=100)    
    last_name = forms.CharField(max_length=100)

signals.post_save.connect(create_profile, sender=User)

From members.signals.py:

def create_profile(sender, instance, signal, created, **kwargs):
    """When user is created also create a matching profile."""

    from basic.members.models import Profile

    if created:
        Profile(user = instance).save()

And members.regbackend.py:

def create_profile(sender, instance, signal, created, **kwargs):
    """When user is created also create a matching profile."""

    from basic.members.models import Profile

    if created:
        Profile(user = instance).save()

I am stuck. Basically my ProfileForm is adding fields to User Registration page but on saving, it is creating a User object with the username an email address but with First and Last name either in the User Object nor the Profile Object are not saved. However a blank Profile is created with the blank fields. I must be missing something small but I can't seem to see it.

So the signals are working but the data is not being saved into the profile.

Honestly, this solution in to our beloved Django should all be in one place with best practice put together for ease.

Help.


回答1:


Where is your Member model defined? I can't even see it being imported for your ProfileForm. Are you putting the ProfileForm and UserRegistrationForm on the same page?

If users are filling out both forms (or even filling out the ProfileForm), you don't need a signal - in your save() for your ProfileForm, just have it create the profile for you:

   def save(self, user):
        try:    
            data = user.get_profile()
        except:
            data = Profile(user=user)
            data.first_name = self.cleaned_data["first_name"]
            data.first_name = self.cleaned_data["last_name"]
            data.save()


来源:https://stackoverflow.com/questions/4615353/django-registration-creating-blank-django-profiles-using-signals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!