Я создал модель с тремя разными внешними ключами и создал четыре формы с одной и той же моделью, и я пытаюсь сохранить одно и то же имя emplooyeename в нескольких формах, которые не хранятся.
Упомянуто все подробно в надежде, что вы сможете Понимаю. Models.py
class UserTimezone(models.Model):
employeename = models.ForeignKey(Enrollment,db_column='EmployeeName',related_name="enrollment", on_delete=models.SET_NULL,null=True)
on_delete=models.SET_NULL,null=True)
doorname = models.ForeignKey(DoorInfo,db_column='DoorName',related_name="doornames",on_delete=models.CASCADE)
timezonename = models.ManyToManyField(TimeZone,db_column='TimezoneName', related_name="timezone")
Forms.py Здесь я сделал четыре формы с той же моделью.
class UserTimezoneForm1(forms.ModelForm):
class Meta:
model = UserTimezone
fields = "__all__"
def __init__(self, *args, **kwargs):
super(UserTimezoneForm1, self).__init__(*args, **kwargs)
self.fields['timezonename'].widget = CheckboxSelectMultiple()
self.fields['timezonename'].queryset = TimeZone.objects.all()
class UserTimezoneForm2(forms.ModelForm):
class Meta:
model = UserTimezone
fields = "__all__"
# widgets = {'employeename': forms.HiddenInput()}
exclude = ["employeename"]
def __init__(self, *args, **kwargs):
super(UserTimezoneForm2, self).__init__(*args, **kwargs)
self.fields['timezonename'].widget = CheckboxSelectMultiple()
self.fields['timezonename'].queryset = TimeZone.objects.all()
class UserTimezoneForm3(forms.ModelForm):
class Meta:
model = UserTimezone
fields = "__all__"
exclude = ["employeename"]
def __init__(self, *args, **kwargs):
super(UserTimezoneForm3, self).__init__(*args, **kwargs)
self.fields['timezonename'].widget = CheckboxSelectMultiple()
self.fields['timezonename'].queryset = TimeZone.objects.all()
Views.py
Пытался очистить данные о сотрудниках во всех формах но сохраняя только первое значение формы.
def usertimezone(request,*args,**kwargs):
user_data = UserTimezone.objects.all()
if request.method == "POST":
form1 = UserTimezoneForm1(request.POST,prefix='form1')
if form1.is_valid():
obj = UserTimezone()
obj.employeename = form1.cleaned_data["employeename"]
obj.doorname = form1.cleaned_data['doorname']
form1.save()
form2 = UserTimezoneForm2(request.POST,prefix='form2')
form1 = UserTimezoneForm1(request.POST,prefix='form1')
if form2.is_valid():
obj = UserTimezone()
#obj.employeename = form2.cleaned_data["employeename"]
obj.doorname = form2.cleaned_data['doorname']
form2.save()
form3 = UserTimezoneForm3(request.POST,prefix='form3')
if form3.is_valid():
obj = UserTimezone()
# obj.employeename = form3.cleaned_data["employeename"]
obj.doorname = form3.cleaned_data['doorname']
form3.save()
form4 = UserTimezoneForm4(request.POST,prefix='form4')
if form4.is_valid():
obj = UserTimezone()
# obj.employeename = form4.cleaned_data["employeename"]
obj.doorname = form4.cleaned_data['doorname']
form4.save()
return redirect(usertimezone)
else:
form1 = UserTimezoneForm1(prefix="form1")
form2 = UserTimezoneForm2(prefix="form2")
form3 = UserTimezoneForm3(prefix="form3")
form4 = UserTimezoneForm4(prefix="form4")
context = {'form1':form1,'form2':form2,'form3':form3,
'form4':form4,'user_data':user_data}
return render(request,'usertimezone.html',context)
Хранение данных таким образом:
emloyeename empid door1 id
None None door2 id
None None door3 id
None None door4 id
how can i store same 'employeename' in place of 'None'.
Любой может помочь мне с этой проблемой, Помощь оценена!