-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.txt
129 lines (108 loc) · 3.8 KB
/
history.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
mkdir django_signals
cd django_signals
python -m venv ENV
source ENV/bin/activate
(source /home/j3/django_signals/ENV/bin/activate)
pip install django
python manage.py createproject signals
cd signal
python manage.py createapp base
cd base/model.py
Type these lines & hit save:
----------------------------------------
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True)
first_name = models.CharField(max_length=200, blank=True, null=True)
last_name = models.CharField(max_length=200, blank=True, null=True)
phone = models.CharField(max_length=200, blank=True, null=True)
def __str__(self):
return str(self.user)
'''
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
print('****************')
print('Profile created!')
print('****************')
post_save.connect(create_profile, sender=User)
def update_profile(sender, instance, created, **kwargs):
if created is False:
instance.profile.save()
print('****************')
print('Profile Updated!')
print('****************')
post_save.connect(update_profile, sender=User)
'''
----------------------------------------
This model connect Django User + my Profile ;)
Now Go to admin.py:
----------------------------------------
from django.contrib import admin
from base.models import Profile
admin.site.register(Profile)
----------------------------------------
Go to apps.py
----------------------------------------
from django.apps import AppConfig
class BaseConfig(AppConfig):
# default_auto_field = 'django.db.models.BigAutoField'
name = 'base'
def ready(self):
import base.signals
----------------------------------------
Create & Go to signals.py:
----------------------------------------
from django.contrib.auth.models import User
from base.models import Profile
from django.db.models.signals import post_save
from django.dispatch import receiver
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
print('****************')
print('Profile created!')
print('****************')
# post_save.connect(create_profile, sender=User)
@receiver(post_save, sender=User)
def update_profile(sender, instance, created, **kwargs):
if created is False:
instance.profile.save()
print('****************')
print('Profile Updated!')
print('****************')
----------------------------------------
python manage.py makemigrations
python manage.py migrate
pip freeze > requirement.txt
code .
exit
Inside vscode:
Go to /admin
Create & Login as superuser
create/ update a user
SEE Profile...
Output:
--------------------------
[05/Apr/2023 02:25:04] "GET /admin/auth/user/add/ HTTP/1.1" 200 7383
[05/Apr/2023 02:25:04] "GET /admin/jsi18n/ HTTP/1.1" 200 3195
****************
Profile created!
****************
[05/Apr/2023 02:25:35] "POST /admin/auth/user/add/ HTTP/1.1" 302 0
[05/Apr/2023 02:25:35] "GET /admin/auth/user/23/change/ HTTP/1.1" 200 16008
[05/Apr/2023 02:25:35] "GET /admin/jsi18n/ HTTP/1.1" 200 3195
****************
Profile Updated!
****************
[05/Apr/2023 02:26:01] "POST /admin/auth/user/23/change/ HTTP/1.1" 302 0
[05/Apr/2023 02:26:01] "GET /admin/auth/user/ HTTP/1.1" 200 8968
[05/Apr/2023 02:26:01] "GET /admin/jsi18n/ HTTP/1.1" 200 3195
--------------------------
There you have!
Hope this will be helpful!
j3
March, 2023