In this part, we will focus on building the Feed App and all its models related to Likes, Comments, posts and all the relevant forms and views.

So, in the 2nd part and the 3rd part of the tutorial series, we have discussed Users app backend and its templates respectively.

So, in this part (the 4th one), we will start discussing Feed app. In this part, we will focus on the backend of Feed app (models, views, forms etc) and we will discuss templates of Feed app in the next part.

Before moving forward, I want to make sure that you have read all the previous parts of the series otherwise it would not make much sense reading this one, though you can if you want to focus on specific details only rather than the complete website.

As I mentioned in previous parts, I won’t be going in details about various terms as it would make it too long. I would focus on the main aspects of the code rather than indulging in simple terms.

To better understand everything, you should be familiar with Django terms. Hopefully, you would be familiar with them from 2nd part of the series but still, if you have doubts, Django official website is a good place to learn about them.

So, let’s go ahead and build the Feed app we created in the first part of the tutorial. We will start by creating the models first.

models.py

In this python file, we will define our models. As you might already know, models are defined to tell Django what to save in the database and to define the relationship between different models and what characteristics they have.

To learn more about Django models, do visit this amazing tutorial on models by Mozilla Developers. It talks about models in depth.

After you are comfortable with how models work, you can proceed to make models for our social media website.

So, in this Feed app, we will have three models — one for posts, one for comments and the last one for likes.

So, as usual, we will have to import various items first up. These would include the default Django User model and timezone.

So, let’s have a look at our first model — Post model. It will have five parameters:-

  1. description — This is the part of the post where the user would put a small description relevant to the picture he is posting. It is optional since we do not want to force the user to put a description. It has a maximum length of 255 characters and is a CharField.
  2. pic — This is the most important part of the post — the picture. Users will upload a picture of their choice for uploading. It would be saved in the file path mentioned. It uses an ImageField.
  3. date_posted — It will use the DateTimeField of Django and will set the timestamp to each post. We will use the default time as the current time.
  4. user_name — This is a ForeignKey relationship. It is a Many to One relationship since a user can have many posts but a post can only belong to one user. When the user is deleted, the post will be deleted too as evidenced by the usage of _on_delete=models.CASCADE. _It links up the post with the User model.
  5. tags — This is used to take in relevant tags for the post. It can be left blank and is of the maximum of 100 characters. Tags can help to search for relevant posts.

Next, we describe the_ str_ which decides how Django will show our model in the admin panel. We have set it to show the description as the Query object.

We also define the _get_absolute_url _to get the absolute URL for that post.

Next, we have the Comments model. It has four parameters:-

  1. post — This is a foreign key which connects the post and comment. A comment can be for a single post but a single post can have multiple comments. Deletion of the post will delete the comments too.
  2. username — This is a foreign key which relates a comment to the user. When the user is deleted, the comment will also be deleted.
  3. comment — This is the CharField which will hold the relevant comment. It has a maximum character limit of 255 characters.
  4. comment_date — It will use the DateTimeField of Django and will set the timestamp to each comment. We will use the default time as the current time.

Next up, we have our final model — **Likes. **It has two parameters:-

  1. user — It represents the user who has liked the post. Deleting the user deletes the like.
  2. post — It is the post on which the like is given. Deleting the post deletes all its likes too.

So, this sums up our models.py file. Let’s have a look at the code:

from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils import timezone

class Post(models.Model):
	description = models.CharField(max_length=255, blank=True)
	pic = models.ImageField(upload_to='path/to/img')
	date_posted = models.DateTimeField(default=timezone.now)
	user_name = models.ForeignKey(User, on_delete=models.CASCADE)
	tags = models.CharField(max_length=100, blank=True)

	def __str__(self):
		return self.description

	def get_absolute_url(self):
		return reverse('post-detail', kwargs={'pk': self.pk})

class Comments(models.Model):
	post = models.ForeignKey(Post, related_name='details', on_delete=models.CASCADE)
	username = models.ForeignKey(User, related_name='details', on_delete=models.CASCADE)
	comment = models.CharField(max_length=255)
	comment_date = models.DateTimeField(default=timezone.now)

class Like(models.Model):
	user = models.ForeignKey(User, related_name='likes', on_delete=models.CASCADE)
	post = models.ForeignKey(Post, related_name='likes', on_delete=models.CASCADE)

#django #python #web-development #programming #developer

Build a Social Media Website with Django - Feed App Backend
2.55 GEEK