Note that there are some explanatory texts on larger screens.

plurals
  1. POExtending user model form with custom fields
    primarykey
    data
    text
    <p>Upon signup, I'd like to request the user for:</p> <ul> <li>Full name (I want to save it as first and last name though)</li> <li>Company name</li> <li>Email</li> <li>Password</li> </ul> <p>I've read through dozens of similar situations on StackOverflow. In models.py, I extend the User model like so:</p> <pre><code># models.py class UserProfile(models.Model): company = models.CharField(max_length = 50) user = models.OneToOneField(User) def create_user_profile(sender, instance, created, **kwargs): if created: profile, created = UserProfile.objects.get_or_create(user=instance) post_save.connect(create_user_profile, sender=User) </code></pre> <p>Source: <a href="https://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django">Extending the User model with custom fields in Django</a></p> <p>I've also added:</p> <pre><code># models.py class SignupForm(UserCreationForm): fullname = forms.CharField(label = "Full name") company = forms.CharField(max_length = 50) email = forms.EmailField(label = "Email") password = forms.CharField(widget = forms.PasswordInput) class Meta: model = User fields = ("fullname", "company", "email", "password") def save(self, commit=True): user = super(SignupForm, self).save(commit=False) first_name, last_name = self.cleaned_data["fullname"].split() user.first_name = first_name user.last_name = last_name user.email = self.cleaned_data["email"] if commit: user.save() return user </code></pre> <p>And in views.py:</p> <pre><code># views.py @csrf_exempt def signup(request): if request.method == 'POST': form = SignupForm(request.POST) if form.is_valid(): new_user = form.save() first_name, last_name = request.POST['fullname'].split() email = request.POST['email'] company = request.POST['company'], new_user = authenticate( username = email, password = request.POST['password'] ) # Log the user in automatically. login(request, new_user) </code></pre> <p>Right now, it doesn't store the company name. How do I do that?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload