用户编辑个人资料表单不会编辑所有字段,Django

时间:2018-04-04 17:43:38

标签: python django

我使用Django's usercreation表单创建了一个注册表单。 然后我使用注册表创建了EditProfileForm

我的问题是EditProfileForm使用User Model,而出生日期字段位于Profile Model。因此,我可以编辑除Birthdate之外的所有字段,因为它不是来自User Model

如何编辑生日,或创建一个我可以编辑所有字段而不仅仅是用户字段的表单?

我的个人资料模型:

class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.PROTECT)

def __str__(self):
    return self.user.username;

USWEST = 'usw'
USEAST = 'use'
EUROPE = 'eu'
OCEANIA = 'oce'
ASIA = 'as'
SOUTHAMERICA = 'sam'
SOUTHAFRICA = 'saf'
MIDDLEEAST = 'me'

PREF_SERVER_CHOICES = (
    (USWEST, 'US-West'),
    (USEAST, 'US-East'),
    (EUROPE, 'Europe'),
    (OCEANIA, 'Oceania'),
    (ASIA, 'Asia'),
    (SOUTHAMERICA, 'South America'),
    (SOUTHAFRICA, 'South Africa'),
    (MIDDLEEAST, 'Middle-East'),
)

pref_server = models.CharField(
    max_length=3,
    choices=PREF_SERVER_CHOICES,
    default=USWEST,
)

birth_date = models.DateField(null=True, blank=False,)
sessions_played = models.IntegerField(null=False, blank=False, default='0',)
teamwork_commends = models.IntegerField(null=False, blank=False, default='0',)
communication_commends = models.IntegerField(null=False, blank=False, default='0',)
skill_commends = models.IntegerField(null=False, blank=False, default='0',)
positivity_commends = models.IntegerField(null=False, blank=False, default='0',)

FORMS.PY

 from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from apps.api.models import Profile, Feedback
from django.forms import ModelForm

#form to create profile
#RegistrationForm VIEW must be created first as well as URl
class RegistrationForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required = False, help_text='Optional.')
last_name = forms.CharField(max_length=30, required = False, help_text='Optional.')
email = forms.EmailField(max_length=254, required=True, help_text='Required. Enter a valid email address.')
birth_date = forms.DateField(help_text='Required. Format: YYYY-MM-DD')
#Class meta will dictate what the form uses for its fields
class Meta:
    model = User
    fields = (
            'username',
            'first_name',
        'last_name',
        'email',
            'birth_date',
        'password1',
        'password2',
    )



#Function to save form details
def save(self,commit=True):
    if commit:
        #Setting Commit to false,otherwise It will only save the fields existing in UserCreationForm
        user = super(RegistrationForm, self).save(commit=False)
        #Adding additional Fields that need to be saved
        #Cleaned data prevents SQL Injections
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.email = self.cleaned_data['email']
        Profile.birth_date = self.cleaned_data['birth_date']
        user.save()

        return user

# User editing profile details
class EditProfileForm(RegistrationForm):
class Meta:
    model = User
    fields = [
        'email',
        'first_name',
        'last_name',
        'birth_date',
    ]

0 个答案:

没有答案