简体   繁体   English

更新Django Rest中的记录

[英]Update Records In Django Rest

I am following one Django Video Tutorials and there for updating any record in Model he created one view as : 我正在关注一个Django Video Tutorials,在那里更新了Model中的任何记录,他创建了一个视图为:

Class UserProfileUpdateDetail(generics.RetrieveUpdateAPIView):
   model = UserProfile

By above he can fetch any record in Mymodel and update that eg, let say by this url -> /api/UserProfile/1/ 通过以上操作,他可以​​获取Mymodel中的任何记录并更新该记录,例如,通过此url-> / api / UserProfile / 1 /

But When I use the same at my end I am getting this error UserProfileUpdateDetail should either include a queryset attribute, or override the get_queryset() method. 但是,当我在末尾使用相同的命令时,出现此错误UserProfileUpdateDetail should either include a queryset attribute, or override the get_queryset() method.

I think it may because of Django Rest Version ? 我认为可能是因为Django Rest版本吗?

So If I update my code according to Rest Docs : 因此,如果我根据Rest Docs更新代码:

class UserProfileUpdateDetail(generics.RetrieveUpdateAPIView):
    queryset = UserProfile.objects.all()
    serializer_class = UserProfileSerializer

Then I can fetch any record as eg, -> /api/UserProfile/1/ 然后,我可以获取任何记录, eg, -> /api/UserProfile/1/

But when I try to update this this single record not gets updated. 但是,当我尝试更新此单个记录时,不会更新。 LEt say I update the first_name but it says username already exist (But It should update the same record which I have fetched but i think its creating new record). 可以说我更新了first_name,但是它说用户名已经存在(但是它应该更新我获取的相同记录,但是我认为它会创建新记录)。

My Complete Record : 我的完整记录:

Models.py : Models.py:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    subject = models.ManyToManyField('Subjects')
    phone = models.CharField(max_length=20)
    address = models.TextField()

    def __unicode__(self):
        return self.user.username

Serializer.py Serializer.py

class UserProfileSerializer(serializers.ModelSerializer):
    user = UserSerializer()
    # subject = SubjectSerializer(many=True)

    class Meta:
        model = UserProfile
        fields = (
                'user',
                'subject',
                'phone',
                'address',
            )

Views.py Views.py

class UserProfileUpdateDetail(generics.RetrieveUpdateAPIView):
    queryset = UserProfile.objects.all()
    serializer_class = UserProfileSerializer

Urls.py Urls.py

 url(r'^api/user/profile/(?P<pk>[0-9]+)/$', UserProfileUpdateDetail.as_view(), name="user-profile-update"),

Couple of remarks: 几句话:

  • the way your profile serializer is laid down, it has a nested object there: the user. 按照配置文件序列化程序的放置方式,它在那里有一个嵌套对象:用户。 DRF doesn't support nested serializers out-of-the-box, you have to overwrite the create/update methods on your serializer to specify how you wan them to be handled; DRF不支持开箱即用的嵌套序列化器,您必须覆盖序列化器上的create / update方法,以指定要如何处理它们。 further reading here: http://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations . 在此处进一步阅读: http : //www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations

  • I would recommend to instrument the user route and the user serializer to include the profile serializer instead of serializing the profile and then the user from there. 我建议对用户路由和用户序列化程序进行检测,以包括配置文件序列化程序,而不是先序列化配置文件,然后再序列化用户。

  • If you can live with it, have a separate route for profile and user; 如果可以使用,请为个人资料和用户设置一条单独的路线; in some designs this works well, because you will need to alter the profile more often than the user definition. 在某些设计中,此方法效果很好,因为您需要比用户定义更频繁地更改配置文件。 Also, for password reset you will have a separate route, for instance. 另外,例如,对于密码重置,您将有一条单独的路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM