Extending a custom model with Django-Registration

Django Version: 4.0.2 Django-Registration Version: 0.8 Application Name: userAccounts

PROBLEM: I am trying to create a user type called a professor that extends the user model. I can complete the registration process, the problem is that only the user is saved in the database, the professor's table remains empty. Therefore, I think the problem may be in the save method. Some suggest what might be the problem?

SETTINGS.PY

AUTH_PROFILE_MODULE = "userAccounts.Professor"

MODELS.PY - Here I create a model that extends from the user and the sample field

from django.db import models
from django.contrib.auth.models import User

class Professor(models.Model):
  user = models.ForeignKey(User, unique=True)
  exampleField = models.CharField(max_length=100)

FORMS.PY - When the user is saved, I save the new_file in the professor's table.

from django import forms
from registration.forms import RegistrationForm
from models import Professor
from registration.models import RegistrationProfile

class CustomRegistrationForm(RegistrationForm):
  exampleField = forms.CharField()

  def save(self, profile_callback=None):
    new_user = RegistrationProfile.objects.create_inactive_user(
        username=self.cleaned_data['username'],
        password=self.cleaned_data['password1'],
        email = self.cleaned_data['email'])
    new_professor = Professor(
        user=new_user, 
        exampleField=self.cleaned_data['exampleField'])
    new_professor.save()
    return new_user

NOTE. Im after this message: Registration form with profile field

+3
1

-

, !: D shacker: Django-Registration Django-Profile,

-, django- save() . : .

. - , :)

. , . -, . shacker, , , theres :

my forms.py

class RegistrationForm(forms.Form):
    username = forms.RegexField(regex=r'^\w+$',
                            max_length=30,
                            widget=forms.TextInput(attrs=attrs_dict),
                            label=_("Username"),
                            error_messages={'invalid': _("This value must contain only letters, numbers and underscores.")})
    email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
                                                           maxlength=75)),
                         label=_("Email address"))
    password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
                            label=_("Password"))
    password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
                            label=_("Password (again)"))

    #ADITIONAL FIELDS
    #they are passed to the backend as kwargs and there they are saved
    nombre = forms.CharField(widget=forms.TextInput(attrs=attrs_dict),
                            label=_("Nombre"))

__ init__.py(app/user_backend/__ init __. py)

class DefaultBackend(object):
def register(self, request, **kwargs):
        username, email, password, nombre = kwargs['username'], kwargs['email'], kwargs['password1'], kwargs['nombre']
        if Site._meta.installed:
            site = Site.objects.get_current()
        else:
            site = RequestSite(request)
        new_user = RegistrationProfile.objects.create_inactive_user(username, email,
                                                                password, site)
        signals.user_registered.send(sender=self.__class__,
                                 user=new_user,
                                 request=request)

        usuario(user = User.objects.get(username=new_user.username), nombre=nombre).save()
        return new_user

root URLS.PY โ†’ url (r'profiles/', include (' profiles.urls ')),

    url(r'^accounts/registrar_usuario/$',
'registration.views.register',
{'backend': 'Hesperides.apps.accounts.user_regbackend.DefaultBackend','form_class':user_RegistrationForm},        
name='registration_register'
),

mattsnider , pdb. . : shacker dmitko,

+4

Source: https://habr.com/ru/post/1658554/


All Articles