Home > front end >  Could you tell me how to deal with the Django ModelForm foreign key authentication
Could you tell me how to deal with the Django ModelForm foreign key authentication

Time:01-27

I now is a string from the front page, and then I need to use the string to find TestCase foreign key associated data does not exist the inventory data, could you tell me how to deal with ModelForm? "Has prompted a valid option: the choice is not in the options available,", here is my code now, still hope you to help us have a look at, thank you
 
# model
The class TestCase (BaseModel) :
Number=models. ForeignKey (WorkTask, on_delete=models. The CASCADE, verbose_name='task number')
Name=models. CharField (max_length=30, unique=True, verbose_name='use case name') # test case name is the only value
Few=models. CharField (max_length=200, null=True, blank=True, verbose_name='remarks')

The class Meta:
Verbose_name='case information'
Verbose_name_plural=verbose_name

Def the __str__ (self) :
Return the self. The name

# form
# test cases to add
Class AddTestCaseForm (forms. ModelForm) :

The class Meta:
The model=TestCase
Fields=[' number ', 'name', 'few']

Def the clean (self) :
Number=self. Data. The get (' number ', ') # access number
Try:
Worktask=worktask. Objects. The get (number=number) # query whether there is a task number
Self. Cleaned_data [' number ']=worktask # replace number for the model object
Return self. Cleaned_data [' number '] # return cleaned_data
Except the Exception as e:
Raise forms. ValidationError (' task number errors') # if not find the corresponding model objects, capture and throw an error
  • Related