django OneToOneField和ForeignKey有什么区别?
我想在我的系统中实现用户.我知道Django已经有了一个身份验证系统,我一直在阅读文档.但我不知道它们之间的区别
from django.contrib.auth.models import User
class Profile(User):
# others fields
Run Code Online (Sandbox Code Playgroud)
和
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User)
# others fields
Run Code Online (Sandbox Code Playgroud)
我不想知道为什么要使用这一个或另一个,但是在引擎盖下会发生什么.有什么不同?
我正在尝试为用户创建一个小应用程序来建立联系人.我使用django-profiles作为我的个人资料的基础.现在一切正常,直到我尝试提交编辑联系表单,我收到此错误.
Cannot assign "<Contact: Contact object>": "Contact.user" must be a "UserProfile" instance.
Run Code Online (Sandbox Code Playgroud)
作为Django的新手,我甚至不确定我是否采取了正确的方法.我的最终目标是让用户能够添加尽可能多的联系人.任何建议表示赞赏.
扩展用户的我的UserProfile模型如下所示:
class UserProfile(models.Model):
#User's Info
user = models.ForeignKey(User, unique=True)
first_name = models.CharField("first name", max_length=30)
last_name = models.CharField("last name", max_length=30)
home_address = models.CharField(max_length=50)
primary_phone = PhoneNumberField()
city = models.CharField(max_length=50)
state = USStateField()
zipcode = models.CharField(max_length=5)
birth_date = models.DateField()
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
Run Code Online (Sandbox Code Playgroud)
我的联系人模型如下:
class Contact(models.Model):
user = models.ForeignKey(UserProfile)
contact_description = models.CharField("Relation or Description of Contact", max_length=50, blank=True)
contact_first_name = models.CharField("contact first name", max_length=30)
contact_last_name = models.CharField("contact last …Run Code Online (Sandbox Code Playgroud)