Skip to content

Commit

Permalink
One-To-One Link, UserProfile and Migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
huzaifsayed committed Oct 23, 2019
1 parent 45dfa68 commit b2ee589
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 0 deletions.
Empty file added apps/userprofile/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions apps/userprofile/admin.py
@@ -0,0 +1,5 @@
from django.contrib import admin

from .models import Profile

admin.site.register(Profile)
5 changes: 5 additions & 0 deletions apps/userprofile/apps.py
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class UserprofileConfig(AppConfig):
name = 'userprofile'
27 changes: 27 additions & 0 deletions apps/userprofile/migrations/0001_initial.py
@@ -0,0 +1,27 @@
# Generated by Django 2.2.6 on 2019-10-23 20:34

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Profile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('bio', models.TextField(blank=True, max_length=500)),
('phone_number', models.CharField(blank=True, max_length=12)),
('birth_date', models.DateField(blank=True, null=True)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
Empty file.
19 changes: 19 additions & 0 deletions apps/userprofile/models.py
@@ -0,0 +1,19 @@
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True)
phone_number = models.CharField(max_length=12, blank=True)
birth_date = models.DateField(null=True, blank=True)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
3 changes: 3 additions & 0 deletions apps/userprofile/tests.py
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions apps/userprofile/views.py
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
1 change: 1 addition & 0 deletions crm_main/settings.py
Expand Up @@ -45,6 +45,7 @@

LOCAL_APPS = [
'apps.common',
'apps.userprofile',
]

INSTALLED_APPS = DEFAULT_APPS + LOCAL_APPS + THIRD_PARTY_APPS
Expand Down

0 comments on commit b2ee589

Please sign in to comment.