05 – CREATING DJANGO MODEL – INVOICE MANAGEMENT SYSTEM

Spread the love

1. We need to create a table where data will be saved. Below is the table structure. Add this code to models.py

class Invoice(models.Model):
	comments = models.TextField(max_length=3000, default='', blank=True, null=True)
	invoice_number = models.IntegerField(blank=True, null=True)
	invoice_date = models.DateField(auto_now_add=False, auto_now=False, blank=True, null=True)
	name = models.CharField('Customer Name', max_length=120, default='', blank=True, null=True)
	
	line_one = models.CharField('Line 1', max_length=120, default='', blank=True, null=True)
	line_one_quantity = models.IntegerField('Quantity', default=0, blank=True, null=True)
	line_one_unit_price = models.IntegerField('Unit Price (D)', default=0, blank=True, null=True)
	line_one_total_price = models.IntegerField('Line Total (D)', default=0, blank=True, null=True)

	line_two = models.CharField('Line 2', max_length=120, default='', blank=True, null=True)
	line_two_quantity = models.IntegerField('Quantity', default=0, blank=True, null=True)
	line_two_unit_price = models.IntegerField('Unit Price (D)', default=0, blank=True, null=True)
	line_two_total_price = models.IntegerField('Line Total (D)', default=0, blank=True, null=True)

	line_three = models.CharField('Line 3', max_length=120, default='', blank=True, null=True)
	line_three_quantity = models.IntegerField('Quantity', default=0, blank=True, null=True)
	line_three_unit_price = models.IntegerField('Unit Price (D)', default=0, blank=True, null=True)
	line_three_total_price = models.IntegerField('Line Total (D)', default=0, blank=True, null=True)

	line_four = models.CharField('Line 4', max_length=120, default='', blank=True, null=True)
	line_four_quantity = models.IntegerField('Quantity', default=0, blank=True, null=True)
	line_four_unit_price = models.IntegerField('Unit Price (D)', default=0, blank=True, null=True)
	line_four_total_price = models.IntegerField('Line Total (D)', default=0, blank=True, null=True)

	line_five = models.CharField('Line 5', max_length=120, default='', blank=True, null=True)
	line_five_quantity = models.IntegerField('Quantity', default=0, blank=True, null=True)
	line_five_unit_price = models.IntegerField('Unit Price (D)', default=0, blank=True, null=True)
	line_five_total_price = models.IntegerField('Line Total (D)', default=0, blank=True, null=True)

	phone_number = models.CharField(max_length=120, default='', blank=True, null=True)
	total = models.IntegerField(default='0', blank=True, null=True)
	balance = models.IntegerField(default='0', blank=True, null=True)
	timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
	last_updated = models.DateTimeField(auto_now_add=False, auto_now=True, blank=True)
	paid = models.BooleanField(default=False)
	invoice_type_choice = (
			('Receipt', 'Receipt'),
			('Proforma Invoice', 'Proforma Invoice'),
			('Invoice', 'Invoice'),
		)
	invoice_type = models.CharField(max_length=50, default='', blank=True, null=True, choices=invoice_type_choice)

	def __unicode__(self):
		return self.invoice_number

2. Run the following commands for the tables to be created in the database

Prepare the model to be created as a table in the database
python manage.py makemigrations

Next, create the model above as a table in the database
python manage.py migrate

3. Register the model in admin so that we can insert data into the database table.

from .models import Invoice

admin.site.register(Invoice)

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

4 Comments

Leave a Reply

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