Is it possible to combine two model fields in a ModelForm to be entered in one charfield?
My model:
class Sample(models.Model):
request_number = models.PositiveIntegerField()
year = models.PositiveIntegerField()
part = models.CharField(max_length=6, null=True, blank=True)
class Meta:
db_table = "sample"
unique_together = (('request_number', 'year', 'part'),)
def __str__(self):
sample_id = "%s/%s" %(str(self.request_number), str(self.year)[2:])
return(sample_id)
A sample is therefore returned from the model as / or, 123/18
However if I make a ModelForm, I dont want two fields to enter request_number and then year. Instead I would like one charfield for the user to enter 123/18, my form to parse the request_number and year, and for the ModelForm to be validated.
I have tried this as a normal django form, but it does not validate in my view.
Form:
class SingleSampleForm(forms.Form):
sample_id = forms.CharField(
required=True,
label='Sample ID:')
class Meta:
fields = ['sample_id']
def __init__(self, *args, **kwargs):
super(SingleSampleForm, self).__init__()
self.helper = FormHelper()
self.helper.layout = Layout(
Field('sample_id',
css_class="search-form-label",
),
Submit('submit', 'Search sample', css_class='upload-btn')
)
self.helper.form_method = 'POST'
def clean(self):
sample_id = self.cleaned_data['sample_id']
if sample_id:
return sample_id
raise ValidationError('This field is required')
views.py:
class SampleView(View):
sample_form = SingleSampleForm
def get(self, request, *args, **kwargs):
sample_form = self.sample_form()
self.context = {'sample_form': sample_form,}
return render(request,
'results/single_sample_search.html',
self.context)
def post(self, request, *args, **kwargs):
sample_form = self.sample_form(request.POST)
if sample_form.is_valid():
print('Valid')
else:
print('not valid')
self.context = {'sample_form': sample_form,}
return render(request,
'results/single_sample_search.html',
self.context)
Is it possible to use django ModelForm, and create a field that is a combination of the two, which can then be parsed and validated in "clean_request_number" and clean_year" methods? Or why am I going wrong with this custom form?
Thanks