Site icon JnPnote

Following Django Tutorial (CRM)-Part10

This post is for myself to remember how to build Django project.
I did follow the tutorial from the JustDjango Learn website free tutorial.
And this is the fourth tutorial named Getting Started With Django.
I am not going over all the details and descriptions for each part. There are good explanations on video of the JustDjango Learn. So, visit their site and try their tutorials if you need more details. Also, the orders of this post and their video might be different because I put things first what I think should come first.

I am doing this on Windows 10 with just Windows PowerShell. Not using virtual machines at all.

1. Login Required

2. redirect to the login page

LOGIN_URL = "/login"

3. show right info(data) for each user

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

# Create your models here.
class User(AbstractUser):
	pass


class UserProfile(models.Model):
	user = models.OneToOneField(User, on_delete=models.CASCADE)

	def __str__(self):
		return self.user.username


class Lead(models.Model):
	first_name = models.CharField(max_length=20)
	last_name = models.CharField(max_length=20)
	age = models.IntegerField(default=0)
	agent = models.ForeignKey("Agent", on_delete=models.CASCADE)
	

class Agent(models.Model):
	user = models.OneToOneField(User, on_delete=models.CASCADE)
	organisation = models.ForeignKey(UserProfile, on_delete=models.CASCADE)

	def __str__(self):
		return self.user.email

4. Signals

def post_user_created_signal(sender, instance, created, **kwargs):
	print(instance, created)
	if created:
		UserProfile.objects.create(user=instance)


post_save.connect(post_user_created_signal, sender=User)

done for this post

Exit mobile version