下面是测试模型,
class Test(models.Model):
owner = models.ForeignKey(UserProfile, on_delete=models.CASCADE,
related_name='user_test')
text = models.CharField(default='Hello World!!!', max_length=40)
def __str__(self):
return self.owner.user.username
def owner_create_receiver(sender, instance, created, *args, **kwargs):
if created:
obj = Test.objects.create(owner=instance)
obj.save()
post_save.connect(owner_create_receiver, sender=UserProfile)
我试图用{{ request.user.user_test.text }}
打印“文本”,但是它不起作用。我在UserProfile Model中尝试了与{{ request.user.user_profile.text }}
相同的方法,并且可以正常工作。
能否请您帮助我理解为什么我无法获得预期的结果?