Learn Django

Contact App Setup - Create Model

Contacts will be stored in a separate model. Follow these steps to create and configure that model.

Step 1: Create the Contacts Model

Follow these steps to create the Contacts model.

Open /.../crmeasy/crmapp/contacts/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
36
37
38
39
40
from django.db import models
from django.contrib.auth.models import User

from crmapp.accounts.models import Account

from shortuuidfield import ShortUUIDField


class Contact(models.Model):
    uuid = ShortUUIDField(unique=True)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    role = models.CharField(max_length=20)
    phone = models.CharField(max_length=20)
    email = models.EmailField()
    account = models.ForeignKey(Account)
    owner = models.ForeignKey(User)
    created_on = models.DateField(auto_now_add=True)

    class Meta:
        verbose_name_plural = 'contacts'

    @property
    def full_name(self):
        return u'%s %s' % (self.first_name, self.last_name)

    def __unicode__(self):
        return u'%s' % self.full_name

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

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

    @models.permalink
    def get_delete_url(self):
        return 'contact_delete', [self.id]

Step 2: Perform a Migration

With the model created, run the migration commands to update the database.

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

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

Step 3: Activate the Admin Site

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

1
2
3
4
5
6
7
from django.contrib import admin
from .models import Contact

class ContactAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'uuid')

admin.site.register(Contact, ContactAdmin)

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 Contacts 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 the contacts model"

Track your progress with a free account