使用三个嵌套模型创建Model实例? Django Rest 3.1

时间:2015-04-22 04:40:30

标签: django django-rest-framework

我有3个连接型号:用户 - > UserProfile - > BuyerProfile,当用户创建帐户时,我想要制作用户,然后是UserProfile,然后是BuyerProfile。我正在使用ModelSerializer,但是当我使用可浏览的api并发帖时,我得到:

{
    "userprofile": {
        "user": [
            "This field is required."
        ]
    }
}

关于如何做到这一点的任何想法?

class BuyerProfileViewSet(viewsets.ModelViewSet):
    queryset = BuyerProfile.objects.all()
    serializer_class = BuyerProfileSerializer

串行器:

    class UserSerializer(serializers.HyperlinkedModelSerializer):
        class Meta:
            model = User
            fields = ('username',)

class UserProfileSerializer(serializers.HyperlinkedModelSerializer):

    user = UserSerializer()

    class Meta:
        model = UserProfile
        fields = ('user', 'bio','prof_type')

class BuyerProfileSerializer(serializers.HyperlinkedModelSerializer):
    userprofile = UserProfileSerializer()

    class Meta:
        model = BuyerProfile
        fields = ('userprofile', 'company','sitename')

1 个答案:

答案 0 :(得分:1)

将参数library(dplyr) library(tidyr) mydf %>% filter(lane.change == "yes") %>% group_by(Vehicle.ID) %>% mutate(order = 1:n()) %>% select(-Frame.ID, -lane.change) %>% rename(target.lane = Lane, Pvtl = Preceding.Vehicle.ID, FVtl = Following.Vehicle.ID) %>% gather(group, number, target.lane, Pvtl, FVtl) %>% unite(group1, group, order, sep = "") %>% spread(group1, number) %>% left_join(mydf, .) 添加到required=False

的实例化中
UserProfileSerializer