Adding a choice field can be a great way to validate what users can input into the application. If you want them to input only certain values in a field, you might want to consider using a choice field.
This is a short tutorial on how to go about making a choice field in Django.
1. Create a choice list for the invoice application.
invoice_type_choice = (
('Invoice', 'Invoice'),
('Receipt', 'Receipt'),
)
invoice_type = models.CharField(max_length=50, blank=True, null=True, choices=invoice_type_choice)
2. Make migrations and migrate to update the database
./manage.py makemigrations
./manage.py migrate
3. Add the newly created field in the form to allow the users to see it in the application
class InvoiceForm(forms.ModelForm):
class Meta:
model = Invoice
fields = [
---
'invoice_type',
---
]
4. Save and check the data entry form to be sure it works.