Djitter - how to build a twitter clone using Django 2.0 - Part three

Djitter - how to build a twitter clone using Django 2.0 - Part three

Published 08. jun 2018 08:06 by Stein Ove Helset

In this part of the series we're going to create the Django apps and set up the database models.

A Django project consists of apps. And a app consists of models and views. A models is where you describe your data. So the database tables are generated and Django can understand how to interact with them.

The djeeter

To begin with we need to create a app. Run the following command to create the first app.


$ python manage.py startapp djeeterprofile

I like to keep the letters lowercase because it makes a better layout of the project. This command creates a new folder called "djeeterprofile" and inside you will find a couple of files.

  • migrations/ - Django uses this to store files containing information about changes you have done to the models.py.
  • admin.py - Configutation file for setting up Django's built-in app Admin.
  • apps.py - Configuration file for the app.
  • models.py - This is where you configure how the data/orm for the app.
  • tests.py - Here you can write unit tests.
  • views.py - This is where we set up our views to handle requests.

The only file you need to worry about now is the djeeterprofile/models.py. Open it in a editor and write this code. Please don't just copy it, because you will learn so much better if you write it your self.


from django.db import models
from django.contrib.auth.models import User

class DjeeterProfile(models.Model):
  user = models.OneToOneField(User, on_delete=models.DO_NOTHING)
  follows = models.ManyToManyField('self', related_name='followed_by', symmetrical=False)

User.djeeterprofile = property(lambda u: DjeeterProfile.objects.get_or_create(user=u)[0])

We create a DjeeterProfile model to extend the default User model that comes with Django. We need this model to keep track of followers and maybe we need to add additional fields later. The related_name "followed_by" makes it really easy for us to get a users followers. Symmetrical=False means that if I follow you, you don't automatically follow me back.

The last line in this files automatically creates a DjeeterProfile when a user is created so that we don't have to think about creating it our self.

The djeet

The next app we have to create is the Djeet. Run the following command.


$ python manage.py startapp djeet

Open up djeet/models.py in your editor and write the following code.


from django.db import models
from django.contrib.auth.models import User

class Djeet(models.Model):
  user = models.ForeignKey(User, related_name='djeets', on_delete=models.DO_NOTHING)
  body = models.CharField(max_length=255)
  created_at = models.DateTimeField(auto_now_add=True)

  class Meta:
    ordering = ('-created_at',)

The Djeet models is where all the magic happens. A Djeet is the same as a tweet and contains of a user who created it, the body and when it was created. Again we use a related_name on the ForeignKey field, this makes it super easy to get alle the users djeets. We add a entry to the Meta class "ordering = ('-created_at',)". This makes the default ordering of the djeets to newest first.

Before we finish up this part we need to run the code were we register the models in the database.

To add these models to the database we have to register the app in our settings.py file. If you scroll down this file you will find a section called "INSTALLED_APPS". Add the following lines to this block:


'djeeterprofile',
'djeet'

And then in the command line run the following code.


$ python manage.py makemigrations
$ python manage.py migrate

Now our models will be installed in the database and we are ready to start building the sign in and sign up screens and get started with the fun :-) If you ever need to make a change to one of the models in your project you can just run those two commands when you're finished and Python will make sure that the tables in the database are updated properly.

Next part

Share this post

Comments

No comments yet

Add comment