Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Django模型:使用OneToOneField访问相关对象中的父对象属性_Reactjs_Django_Django Models_Django Rest Framework_One To One - Fatal编程技术网

Reactjs Django模型:使用OneToOneField访问相关对象中的父对象属性

Reactjs Django模型:使用OneToOneField访问相关对象中的父对象属性,reactjs,django,django-models,django-rest-framework,one-to-one,Reactjs,Django,Django Models,Django Rest Framework,One To One,我试图从一个由OneToOneField关联的概要文件对象访问Django用户对象的username属性 class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) username = models.TextField(default=user.username, primary_key=True) image = models.ImageField(u

我试图从一个由OneToOneField关联的概要文件对象访问Django用户对象的username属性

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    username = models.TextField(default=user.username, primary_key=True)
    image = models.ImageField(upload_to='images/profile')
    header = models.CharField(max_length=64)
    slug = AutoSlugField(populate_from='x')
    bio = models.CharField(max_length=300, blank=True)
这样做的目的是通过将登录时提供的用户名传递回Django API中的概要细节端点,从而能够使用ReactJS前端获取概要文件对象,其中用户名是端点的主键

路径('/profile/',ShowProfilePageView.as_view(),name='show_profile_page'),

对于传递给Profile username属性的默认参数,我尝试了许多不同的方法,但到目前为止没有任何效果。这可能吗

附录1:ShowProfilePageView视图

class ShowProfilePageView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Profile.objects.all()
    serializer_class = ProfileSerializer
    model = Profile

要访问一对一字段的属性,只需执行以下操作:

profile = Profile.objects.get(pk='profile_pk') # an object of profile
username = profile.user.username
要通过用户名搜索配置文件:

profile = Profile.objects.get(user=User.objects.get(username='username'))

因此,您不需要在
Profile
class

上定义
username
字段,我认为您可以简单地覆盖视图中的
lookup\u字段,如下所示:

class ShowProfilePageView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Profile.objects.all()
    serializer_class = ProfileSerializer
    model = Profile
    lookup_field='user__username'
    lookup_url_kwarg='username'
path('<str:username>/profile/', ShowProfilePageView.as_view(), name='show_profile_page')
并按如下方式更新url:

class ShowProfilePageView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Profile.objects.all()
    serializer_class = ProfileSerializer
    model = Profile
    lookup_field='user__username'
    lookup_url_kwarg='username'
path('<str:username>/profile/', ShowProfilePageView.as_view(), name='show_profile_page')
path('/profile/',ShowProfilePageView.as_view(),name='show_profile_page')

因为通过
lookup\u字段
,视图将从概要文件模型中查找用户模型中的值。而
lookup\u url\u kwargs
是映射它应该从url中使用的值。有关更多信息,请参阅FYI您应该从配置文件模型中删除
username
字段,它应该使用
AutoField
(这是模型中主键的默认字段)。

您可以共享视图
ShowProfilePageView
的代码吗?@ruddra当然,我在上面添加了它。这很有效!我最终不得不重新安排url路径,但查找更改正是我所需要的。非常感谢。