Long trả lời: p
tôi đã tìm thấy The Missing Manual bài vô giá cho các loại hình vấn đề vì nó giải thích nhiều tính năng của django-profile và hệ thống django-đăng ký.
tôi muốn đề nghị sử dụng multi table inheritance trên hồ sơ đơn bạn được phép thiết lập thông qua các AUTH_PROFILE_MODULE
Ví dụ
#models.py
class Profile(models.Model):
#add any common fields here (first_name, last_name and email come from User)
#perhaps add is_student or is_teacher properites here
@property
def is_student(self):
try:
self.student
return True
except Student.DoesNotExist:
return False
class Teacher(Profile):
#teacher fields
class Student(Profile):
#student fields
django đăng ký sử dụng tín hiệu để thông báo cho bạn về một đăng ký. Bạn nên tạo hồ sơ tại thời điểm đó để bạn tự tin rằng các cuộc gọi tới user.get_profile() sẽ luôn trả về một hồ sơ. Mã tín hiệu được sử dụng là
#registration.signals.py
user_registered = Signal(providing_args=["user", "request"])
Điều này có nghĩa là khi xử lý tín hiệu bạn có quyền truy cập yêu cầu. Vì vậy, khi bạn POST biểu mẫu đăng ký bao gồm một trường xác định loại người dùng nào cần tạo.
#signals.py (in your project)
user_registered.connect(create_profile)
def create_profile(sender, instance, request, **kwargs):
from myapp.models import Profile, Teacher, Student
try:
user_type = request.POST['usertype'].lower()
if user_type == "teacher": #user .lower for case insensitive comparison
Teacher(user = instance).save()
elif user_type == "student":
Student(user = instance).save()
else:
Profile(user = instance).save() #Default create - might want to raise error instead
except KeyError:
Profile(user = instance).save() #Default create just a profile
Nếu bạn muốn thêm bất kỳ thứ gì vào mẫu được tạo, không được bao gồm bởi giá trị trường mặc định, tại thời điểm đăng ký, bạn rõ ràng có thể lấy từ yêu cầu.
bạn có một typ o có: P – jonasespelita