S
S
slavamironoff2020-10-22 10:58:18
Django
slavamironoff, 2020-10-22 10:58:18

How to remove table field name override?

Good afternoon.
Extending the User model by adding new fields.

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

#models.py
class Personal(models.Model):
    user = models.OneToOneField(User, on_delete = models.PROTECT)
    middle_name = models.CharField(name='middle_name', verbose_name="Отчество", max_length=255)
    user_adress = models.CharField(name='user_adress', verbose_name="Адресс", max_length=255)
    user_position = models.CharField(name='user_position', verbose_name="Должность", max_length=255)


# forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from .models import Personal
from django.contrib.auth.models import User


class SaveFields(UserCreationForm):
    middle_name = forms.CharField(max_length=255)
    user_adress = forms.CharField(max_length=255)
    user_position = forms.CharField(max_length=255)

    class Meta:
        model=User
        fields=('middle_name', 'user_adress', 'user_position' )


# views.py
def index_add(request):
    # Регистрация
    if request.method == 'POST':
        print(request.POST)
        form = SaveFields(request.POST)
        if request.POST:
            if form.is_valid():
                form.save()
    return render(request, template_name = 'account/signup.html', context={
        'title': 'Сотрудники',
        'form': form
    })


The problem is that the data does not reach the fields that I added.
I noticed that in the admin panel the name of the input was changed to name="personal-0-middle_name", when it should be name="middle_name"
How to redefine the name of the input? so I think that the problem is connected just with this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2020-10-23
@buslay

From documentation:

These profile models are not special in any way - they are just Django models that happen to have a one-to-one link with a user model. As such, they aren't auto created when a user is created, but a django.db.models.signals.post_save could be used to create or update related models as appropriate.

This is just from an example of "extension" of the standard User model.. verbatim - the objects associated with the User through OneToOneField are not created at the time of user creation, but you have the opportunity to do this through Signals.
You still have the option to "redefine" the standard user model, and then the required fields will be immediately in User. For example:
class AuthUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(u'Электронная почта', unique=True)
    date_add = models.DateTimeField(u'Дата добавлен', auto_now_add=True)
    is_active = models.BooleanField(u'активен', default=True)
    employee = models.OneToOneField('person.Employies', null=True, blank=True, editable=True, verbose_name='Профиль сотрудника', on_delete=models.PROTECT)
    is_staff = models.BooleanField(u'администратор', default=False
                                   , help_text=u'определяет возможность входа в панель управления')
    roles = models.ManyToManyField(AuthUserRoles, verbose_name=u'Роли')
    objects = AuthUserManager()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question