Я создал две модели -
class DID_Definition_Model(models.Model): # DID to Region-Carrier Mapping
region_carrier = models.ForeignKey(Telco_Carrier_Mapper_Model, on_delete=models.CASCADE)
did_number= models.CharField(max_length=32, validators=[alphanumeric], primary_key=True)
did_cost= models.DecimalField(max_digits=10, decimal_places=2)
created_on_date = models.DateTimeField(default=timezone.now)
class DID_Number_Assignment_Model(models.Model): #DID Number Assignment
did_selector = models.ForeignKey(DID_Definition_Model, on_delete=models.CASCADE, primary_key=True)
subscriber_department=models.CharField(max_length=200)
usage_assignment=models.ForeignKey(Usage_Assignment_Model, on_delete=models.CASCADE)
employee_email=models.EmailField()
employee_fullname=models.CharField(max_length=200)
created_on_date = models.DateTimeField(default=timezone.now)
И создал представленный ниже вид -
def did_assignment_form(request, did_selector=0):
if request.method =="GET":
if did_selector==0:
form = DID_Number_Assignment_Model_Form()
else:
did_assignment_item = DID_Number_Assignment_Model.objects.get(pk=did_selector)
form = DID_Number_Assignment_Model_Form(instance=did_assignment_item)
return render(request, 'MASTERHANDLER/did_assignment_form.html', {'form':form})
else:
if id==0:
context = DID_Number_Assignment_Model.objects.values('did_selector')
if did_selector not in context:
form = DID_Number_Assignment_Model_Form(request.POST)
else:
did_assignment_item = DID_Number_Assignment_Model.objects.get(pk=did_selector)
form = DID_Number_Assignment_Model_Form(request.POST, instance = did_assignment_item)
if form.is_valid():
form.save()
return redirect('did_assignment_list')
Подробности формы ниже -
class DID_Number_Assignment_Model_Form(forms.ModelForm):
class Meta:
model = DID_Number_Assignment_Model
fields = ('did_selector', 'usage_assignment', 'employee_fullname', 'employee_email', 'subscriber_department' )
labels = {
'did_selector' : 'DID Selector',
'usage_assignment' : 'Number Usage ',
'employee_fullname' : 'Employee Full Name',
'employee_email' : 'Employee Email',
'subscriber_department' : 'Employee Department',
}
def __init__(self, *args, **kwargs):
super(DID_Number_Assignment_Model_Form,self).__init__(*args, **kwargs)
# TO SET drop down default text for a field , optional -
self.fields['did_selector'].empty_label = "Select"
self.fields['usage_assignment'].empty_label = "Select"
# TO SET a field which may be optional -
self.fields['subscriber_department'].required = False
#self.fields['region_assigned'].required = False
Эта форма работает с никаких проблем, но с одной маленькой странностью. Если я создаю объект в DID_Number_Assignment_Model_Form с полем did_selector в качестве внешнего ключа, то же значение «did_selector» будет показано для следующего процесса создания.
Мой вопрос заключается в том, как показать только те, которые сделали значения селектора, которые не были настроены. Пример скриншота ниже -