Learn Django

Account App Setup - Create Model

In this lesson you will create the Account model. This is the model that will be used by the end user to track their accounts.

Step 1: Install the UUID Utility

The account model has a special field that will store a short UUID. A UUID is a unique ID that will be used to identify account records. In order to create UUIDs, we need to install a third-party tool: django-shortuuidfield. Follow these steps to do so.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# navigate to crmeasy directory (if not already there)
$ cd /<your_path_here>/crmeasy

# activate the virtual environment (if not already activated)
$ source venv/bin/activate

# install the utility
(venv)$ pip install django-shortuuidfield

# update requirements.txt
(venv)$ pip freeze > requirements.txt

Step 2: Create the Accounts Model

Follow these steps to create the Accounts model.

Open /.../crmeasy/crmapp/accounts/models.py in your IDE. Delete it's contents and type in the following.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from django.db import models
from django.contrib.auth.models import User

from shortuuidfield import ShortUUIDField


class Account(models.Model):
    uuid = ShortUUIDField(unique=True)
    name = models.CharField(max_length=80)
    desc = models.TextField(blank=True)
    address_one = models.CharField(max_length=100)
    address_two = models.CharField(max_length=100, blank=True)
    city = models.CharField(max_length=50)
    state = models.CharField(max_length=2)
    phone = models.CharField(max_length=20)
    owner = models.ForeignKey(User)
    created_on = models.DateField(auto_now_add=True)

    class Meta:
        verbose_name_plural = 'accounts'

    def __unicode__(self):
        return u"%s" % self.name

    @models.permalink
    def get_absolute_url(self):
        return 'account_detail', [self.uuid]

    @models.permalink
    def get_update_url(self):
        return 'account_update', [self.uuid]

    @models.permalink
    def get_delete_url(self):
        return 'account_delete', [self.uuid]

Code Review

The above model should look familiar. It's very similar to how the Subscriber model was created. Instead of reviewing the code in it's entirety, only the new concepts will be covered.

Line 8: This defines the UUID field. We pass in the unique=True to ensure that no two records have the same ID.

Lines 26-27: This defines the get_absolute_url() method for the model. When used, this method will return the URL address to the model instance be referenced.

For an example of this is used, refer to the Accounts List template (which you'll be coding later in this chapter). On line 22 you'll see {{ account.get_absolute_url }} - this is calling the get_absolute_url() method of the model object.

Lines 29-31: Like the statement above it, this block of code is creating a shortcut that can be called on the object. Instead of returning the Account Detail URL, this provides a shortcut to the Account Update URL.

Lines 33-35: Like the other two statements above it, however it defines a link to the Account Delete URL.

Step 3: Perform a Migration

With the model created, run the migration commands to commit it to the database. Make sure you're in the /crmeasy/ folder.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# create the migration
(venv)$ python manage.py makemigrations accounts
Migrations for 'accounts':
  0001_initial.py:
    - Create model Account

# commit the migration
(venv)$ python manage.py migrate accounts
Operations to perform:
  Apply all migrations: accounts
Running migrations:
  Applying accounts.0001_initial... OK

Step 4: Activate the Admin Site

Next, activate the admin site by opening /.../crmeasy/crmapp/accounts/admin.py in your IDE. Type in the following code.

1
2
3
4
from django.contrib import admin
from .models import Account

admin.site.register(Account)

Step 4: Create a Test Record

The next step is to add a test account record to verify if the model was setup correctly. You can do so using the following steps.

First, make sure the development server is running.

1
2
3
4
5
6
7
8
# navigate to crmeasy directory (if not already there)
$ cd /<your_path_here>/crmeasy

# activate the virtual environment (if not already activated)
$ source venv/bin/activate

# start Django's development web server
(venv)$ python manage.py runserver 8000

Now you can add a record using these steps.

  1. Make sure the development server is running
  2. Open http://127.0.0.1:8000/admin
  3. Login with your admin credentials if prompted
  4. Click on the Accounts model
  5. Click the ‘Add’ button in the upper right hand corner
  6. Fill in all the required fields and click the ‘Save’ button

Step 5: Commit Changes

Execute these commands to commit your changes in Git.

1
2
3
4
5
# add files
(venv)$ git add .

# commit files
(venv)$ git commit -m "created accounts model"

Track your progress with a free account