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.
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:-
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:-
Next up, we have our final model — **Likes. **It has two parameters:-
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