我有两种形式(student_form和alumni_form),每一种都传递初始数据,其中我的模型中的学生模型和校友模型与OneToOne相关,并且作为用户个人资料,因此我需要使用这两种形式来更新数据,但不幸的是只有用户模型数据已更新,但学生和校友的数据未更新,但是保存方法的工作就像将数据保存到数据库一样。任何帮助,请
view.py
def alumni_update(request, user_id):
# user = get_object_or_404(User, pk=user_id)
user = get_object_or_404(User, pk=user_id)
# get student_user profile and alumni_student profile
profile_student = user.student
profile_alumni = profile_student.alumni
initial_student = {'st_reg_no': profile_student.st_reg_no, 'st_other_name': profile_student.st_other_name,
'st_dob': profile_student.st_dob, 'st_gender': profile_student.st_gender,
'st_phone_number': profile_student.st_phone_number,
'st_nationality': profile_student.st_nationality,
'st_city': profile_student.st_city, 'programme': profile_student.programme,
'campus': profile_student.campus,
}
initial_alumni = {
'alumni_type': profile_alumni.alumni_type, 'graduation_year': profile_alumni.graduation_year,
}
if request.method == 'POST':
student_form = StudentUpdateForm(request.POST, initial=initial_student, instance=user)
alumni_form = AlumniUpdateForm(request.POST, initial=initial_alumni, instance=profile_student)
else:
student_form = StudentUpdateForm(initial=initial_student, instance=user)
alumni_form = AlumniUpdateForm(initial=initial_alumni, instance=profile_student)
return save_user_form(request, student_form, alumni_form, 'partials/alumni/partial_alumni_update.html')
def save_user_form(request, student_form, alumni_form, template_name):
data = dict()
if request.method == 'POST':
if student_form.is_valid() and alumni_form.is_valid():
student_form.save()
alumni_form.save()
data['form_is_valid'] = True
users = User.objects.all().filter(is_student__exact=1).order_by('-date_joined')
data['html_alumni_list'] = render_to_string('partials/alumni/partial_alumni_list.html', {
'my_user_list': users,
'current_user': request.user
})
else:
data['form_is_valid'] = False
context = {'student_form': student_form, 'alumni_form': alumni_form}
data['html_form'] = render_to_string(template_name, context, request=request)
return JsonResponse(data)
models.py
class Student(models.Model):
user = models.OneToOneField('User', on_delete=models.CASCADE, primary_key=True, related_name='student')
st_reg_no = models.CharField(max_length=200)
st_other_name = models.CharField(max_length=200)
st_dob = models.CharField(max_length=10)
gender_choice = (
('M', 'Male'),
('F', 'Female')
)
st_gender = models.CharField(max_length=1, choices=gender_choice, blank=True, default='M')
st_phone_number = models.CharField(max_length=200, blank=True, null=True)
st_nationality = CountryField(blank_label='(select country)')
st_city = models.CharField(max_length=70)
programme = models.ForeignKey('Programme', on_delete=models.SET_NULL, null=True)
campus = models.ForeignKey('Campus', on_delete=models.SET_NULL, null=True)
st_image = models.ImageField(upload_to='student_pics', blank=True)
class Alumni(models.Model):
alumni_choice = (
('IDM', 'Institute of Development Management'),
('MZ', 'Mzumbe University')
)
alumni_type = models.CharField(max_length=3, choices=alumni_choice, blank=True, default='MZ')
YEAR_CHOICES = []
for r in range(1980, (datetime.datetime.now().year + 1)):
YEAR_CHOICES.append((r, r))
graduation_year = models.IntegerField('year', choices=YEAR_CHOICES, default=datetime.datetime.now().year)
# graduation_year = models.DateField()
employer = models.CharField(max_length=200, null=True, blank=True)
title = models.CharField(max_length=200, null=True, blank=True, help_text='Alumni Designation or Title')
description = models.TextField(help_text='Enter a brief description of the title', max_length=1000)
# check ******************
student = models.OneToOneField('Student', on_delete=models.CASCADE, primary_key=True)
forms.py
class StudentUpdateForm(UserChangeForm):
password = None
st_reg_no = forms.CharField(label='Student Reg No', max_length=200, required=True)
st_other_name = forms.CharField(label='Other Name', max_length=200, required=False)
st_dob = forms.DateField(label='Date Of Birth', widget=forms.DateInput(attrs={'class': 'dob_datepicker'}))
# st_dob = forms.CharField(label='Date Of Birth', widget=forms.TextInput(attrs={'class': 'dob_datepicker'}))
gender_choice = (
('M', 'Male'),
('F', 'Female')
)
st_gender = forms.ChoiceField(label='Gender', choices=gender_choice)
st_phone_number = forms.CharField(label='Phone Number', max_length=200, required=False)
st_nationality = forms.ChoiceField(choices=COUNTRY_CHOICES, required=True, label='Nationality',
widget=forms.Select(attrs={'class': 'my_select2'}))
st_city = forms.CharField(label='City', max_length=70)
programme = ProgrammeChoiceField(queryset=Programme.objects.all(),
widget=forms.Select(attrs={'class': 'my_select2'}))
campus = CampusChoiceField(queryset=Campus.objects.all(), widget=forms.Select(attrs={'class': 'my_select2'}))
st_image = forms.ImageField(label='Upload Picture', required=False)
class Meta(UserChangeForm.Meta):
model = User
fields = ('username', 'email', 'first_name', 'last_name')
class AlumniUpdateForm(forms.ModelForm):
alumni_choice = (
('IDM', 'Institute of Development Management'),
('MZ', 'Mzumbe University')
)
alumni_type = forms.ChoiceField(choices=alumni_choice)
YEAR_CHOICES = []
for r in range(1980, (datetime.datetime.now().year + 1)):
YEAR_CHOICES.append((r, r))
graduation_year = forms.ChoiceField(choices=YEAR_CHOICES, widget=forms.Select(attrs={'class': 'my_select2'}))
employer = forms.CharField(max_length=200, required=False)
title = forms.CharField(max_length=200, required=False)
description = forms.CharField(
required=False,
label='Description',
widget=forms.Textarea(attrs={'placeholder': 'Your Descriptions goes here', 'rows': 5, 'cols': 100})
)
class Meta:
model = Alumni
fields = ('alumni_type', 'graduation_year', 'employer', 'title', 'description')