[英]Getting inherited class fields django
I am creating a custom user and profile model for my django application. 我正在为django应用程序创建自定义用户和配置文件模型。 I have created a user model and another profile model with
OneToOneField
to user model. 我已经使用
OneToOneField
创建了一个用户模型和另一个配置文件模型。 I want to get profile model field values in template but only i can get object of user model in views from currently logged in user. 我想在模板中获取概要模型字段值,但是只有我可以从当前登录的用户的视图中获取用户模型的对象。 How can I get profile model field values in the template ?
如何在模板中获取概要模型字段值?
models.py models.py
class UserSignup(models.Model):
email = models.CharField(unique=True, max_length=254)
name = models.CharField(max_length=100)
password = models.CharField(max_length=1000)
created_date = datetime.now()
def __str__(self):
return self.email
class UserProfile(models.Model):
user = models.OneToOneField(UserSignup, on_delete=models.CASCADE)
full_name = models.CharField(max_length=200, null=True)
#photo = models.ImageField(upload_to="/user/image/", blank=True)
birth_date = models.DateField(null=True)
books_wrote = models.TextField(null=True)
journal_wrote = models.TextField(null=True)
address = models.TextField(max_length=2000, null=True)
@receiver(post_save, sender=UserSignup)
def update_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
views.py views.py
def user_profile(request):
op = UserSignup.objects.get(id=request.session['uid'])
#here i want object of profile so i can access fields of profile in template
return render(request, 'editor_profile.html', {'op': op})
template 模板
<div class="panel-heading">
<h3 class="panel-title">{{ op.name }}</h3>
</div>
<div class=" col-md-9 col-lg-9 ">
<table class="table table-user-information">
<tbody>
<tr>
<td>Full Name:</td>
<td>{{ op.full_name }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
You could access the OneToOne
related UserProfile
model fields like, 您可以访问与
OneToOne
相关的UserProfile
模型字段,例如,
{{ op.userprofile.full_name }}
{{ op.userprofile.birth_date }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.