20 – HOW TO ADD A CHOICE FIELD IN DJANGO – INVOICE MANAGEMENT SYSTEM

Spread the love

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.


Spread the love

About the author

arbadjie

Hi, I'm Abdourahman Badjie, an aspiring developer with obsession for anything coding and networking. This blog is dedicated to helping people learn to develop web applications using django framework.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *