admin py from django contrib auth models import User from django db im

 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
#admin.py
from django.contrib.auth.models import User
from django.db import models
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from account.models import UserProfile
class ProfileInline(admin.StackedInline):
model = UserProfile
extra = 1
max_num = 1
class ProfileAdmin(admin.ModelAdmin):
inlines = (ProfileInline, )
admin.site.unregister(User)
admin.site.register(User, ProfileAdmin)
#models.py
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User, UserManager
from django.db.models import signals
GENDER_CHOICES = (
('M', 'Мужской'),
('F', 'Женский'),
)
class UserProfile(User):
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
birthday = models.DateField()
country = models.IntegerField()
city = models.IntegerField()
objects = UserManager()
def create_profile_for_user(sender, **kwargs):
if kwargs['created']:
p = UserProfile() # 'UserProfile' is my custom User model
p.__dict__.update(kwargs['instance'].__dict__)
p.save()
class Country(models.Model):
country = models.CharField(max_length=100)
class City(models.Model):
country = models.ForeignKey(Country)
city = models.CharField(max_length=100)
signals.post_save.connect(create_profile_for_user, sender=User)