如何创建一个 UserProfile 字段,它是 User 字段的别名?
How to create a UserProfile field which is alias for a User field?
我想访问 User 模型的 first_name、last_name 和来自 UserProfile 的电子邮件字段,就好像它们是 UserProfile 自己的字段一样。
class Person(models.Model):
user = models.OneToOneField(User)
name = #ModelField which refers to user.first_name
surname = #ModelField which refers to user.last_name
email = #ModelField which refers to user.email
birth_date = models.DateField(null=True, blank=True)
#and other fields
我可以使用独立的名字、姓氏、电子邮件、字段,但它会导致用户字段的数据重复。
upd
我希望 name
、surname
和 email
属于某些 Field
类型(如 birth_date)并将这些字段修改为等同修改相应的用户字段(属性 行为如下所示)。我需要这个,因为我希望这三个字段在管理界面中可食用,或者与 ModelForm
中的 "native" 字段同等处理。
upd解决方法。不是很花哨,但它对我有用:
class Person(models.Model):
user = models.OneToOneField(User)
#and also some char, text fields
@property
def name(self):
return self.user.first_name
@name.setter
def name(self, value):
self.user.first_name = value
#and by analogy surname and email properties
class PersonForm(ModelForm):
class Meta:
model = Person
exclude = ('user',)
name = forms.CharField(max_length=100, required=False)
surname = forms.CharField(max_length=100, required=False)
email = forms.EmailField(required=False)
#no make fields filled with User data on form load
def __init__(self, *args, **kwargs):
if 'instance' in kwargs:
instance = kwargs['instance']
initial = kwargs.get('initial', {})
initial['name'] = instance.name
initial['surname'] = instance.surname
initial['email'] = instance.email
kwargs['initial'] = initial
super(PersonForm, self).__init__(*args, **kwargs)
# to save form data to User model when commit
def save(self, commit=True):
instance = super(PersonForm, self).save(commit)
user = instance.user
user.first_name = self.cleaned_data['name']
user.last_name = self.cleaned_data['surname']
user.email = self.cleaned_data['email']
user.save()
return instance
class PersonAdmin(admin.ModelAdmin):
fields = ['name', 'surname', 'email', 'and_others']
form = PersonForm
admin.site.register(Person, PersonAdmin)
class Person(models.Model):
user = models.OneToOneField(User)
@property
def name(self):
return self.user.first_name
@property
def surname(self):
return self.user.last_name
如果您想要访问 user.first_name、user.last_name 等...直接作为 Person 的属性,您可以向 Person 模型添加名称、姓氏等...属性,而不是字段:
class Person(models.Model):
user = models.OneToOneField(User)
birth_date = models.DateField(null=True, blank=True)
#and other fields
@property
def name(self):
return self.user.first_name
@property
def surname(self):
return self.user.last_name
....
我想访问 User 模型的 first_name、last_name 和来自 UserProfile 的电子邮件字段,就好像它们是 UserProfile 自己的字段一样。
class Person(models.Model):
user = models.OneToOneField(User)
name = #ModelField which refers to user.first_name
surname = #ModelField which refers to user.last_name
email = #ModelField which refers to user.email
birth_date = models.DateField(null=True, blank=True)
#and other fields
我可以使用独立的名字、姓氏、电子邮件、字段,但它会导致用户字段的数据重复。
upd
我希望 name
、surname
和 email
属于某些 Field
类型(如 birth_date)并将这些字段修改为等同修改相应的用户字段(属性 行为如下所示)。我需要这个,因为我希望这三个字段在管理界面中可食用,或者与 ModelForm
中的 "native" 字段同等处理。
upd解决方法。不是很花哨,但它对我有用:
class Person(models.Model):
user = models.OneToOneField(User)
#and also some char, text fields
@property
def name(self):
return self.user.first_name
@name.setter
def name(self, value):
self.user.first_name = value
#and by analogy surname and email properties
class PersonForm(ModelForm):
class Meta:
model = Person
exclude = ('user',)
name = forms.CharField(max_length=100, required=False)
surname = forms.CharField(max_length=100, required=False)
email = forms.EmailField(required=False)
#no make fields filled with User data on form load
def __init__(self, *args, **kwargs):
if 'instance' in kwargs:
instance = kwargs['instance']
initial = kwargs.get('initial', {})
initial['name'] = instance.name
initial['surname'] = instance.surname
initial['email'] = instance.email
kwargs['initial'] = initial
super(PersonForm, self).__init__(*args, **kwargs)
# to save form data to User model when commit
def save(self, commit=True):
instance = super(PersonForm, self).save(commit)
user = instance.user
user.first_name = self.cleaned_data['name']
user.last_name = self.cleaned_data['surname']
user.email = self.cleaned_data['email']
user.save()
return instance
class PersonAdmin(admin.ModelAdmin):
fields = ['name', 'surname', 'email', 'and_others']
form = PersonForm
admin.site.register(Person, PersonAdmin)
class Person(models.Model):
user = models.OneToOneField(User)
@property
def name(self):
return self.user.first_name
@property
def surname(self):
return self.user.last_name
如果您想要访问 user.first_name、user.last_name 等...直接作为 Person 的属性,您可以向 Person 模型添加名称、姓氏等...属性,而不是字段:
class Person(models.Model):
user = models.OneToOneField(User)
birth_date = models.DateField(null=True, blank=True)
#and other fields
@property
def name(self):
return self.user.first_name
@property
def surname(self):
return self.user.last_name
....