Learn Django

Comm App Setup - Create Model

All communications will be stored in their own model. This model is mostly like the others you've created so far. The only difference is that it has a drop-down menu selection. You can see how this is setup by seeing how we set the value of the TYPE_LIST list, and how that list is passed into the kind field.

Step 1: Create the Communications Model

Follow these steps to create the Accounts model.

Open /.../crmeasy/crmapp/communications/models.py in your IDE. Delete its 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 Communication(models.Model):
    uuid = ShortUUIDField(unique=True)
    TYPE_LIST = (
        (1, 'Meeting'),
        (2, 'Phone'),
        (3, 'Email'),
    )
    subject = models.CharField(max_length=50)
    notes = models.TextField()
    kind = models.PositiveSmallIntegerField(choices=TYPE_LIST)
    date = models.DateField()
    owner = models.ForeignKey(User)
    account = models.ForeignKey(Account)
    created_on = models.DateField(auto_now_add=True)

    class Meta:
        verbose_name_plural = 'communications'

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

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

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

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

Step 3: Perform a Migration

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

  1. Open terminal, make sure it's activated, and navigate to the /.../crmeasy folder
  2. Execute these commands
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# create the migration
(venv)$ python manage.py makemigrations communications
Migrations for 'communications':
  0001_initial.py:
    - Create model Communication

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

Step 3: Activate the Admin Site

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

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

class CommunicationAdmin(admin.ModelAdmin):
    list_display = ('subject', 'uuid')

admin.site.register(Communication, CommunicationAdmin)

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 Communications 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 communications model"

Track your progress with a free account