Django API:创建序列化程序Issu

2024-06-26 00:12:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发我的Django web应用程序,并开始API部分。 我有一个创建序列化程序类,如下所示:

class IndividuCreateSerializer(serializers.ModelSerializer) :
    class Meta :
        model = Individu
        fields = [
            'Etat',
            'Civilite',
            'Nom',
            'Prenom',
            'Sexe',
            'Statut',
            'DateNaissance',
            'VilleNaissance',
            'PaysNaissance',
            'Nationalite1',
            'Nationalite2',
            'Profession',
            'Adresse',
            'Ville',
            'Zip',
            'Pays',
            'Mail',
            'Telephone',
            'Image',
            'CarteIdentite',
            ]

    def create(self, validated_data):
        obj = Individu.objects.create(**validated_data)
        IdentityIndividuResumeView.get_context_data(obj.id)
        return obj

在这个类中,我有我的create function,当我的人被创建时,它应该重定向到IdentityIndividuResumeView class。你知道吗

class IdentityIndividuResumeView(LoginRequiredMixin, TemplateView) :

    template_name = 'Identity_Individu_Resume.html'
    model = Individu

    def get_context_data(self, **kwargs) :

        context_data = super(IdentityIndividuResumeView, self).get_context_data(**kwargs)

        id = self.kwargs['id']
        personne = get_object_or_404(Individu, pk=id)

        NIU = lib.Individu_Recherche.NIUGeneratorIndividu(personne)
        personne.NumeroIdentification = NIU
        ...

但我无法在函数get_context_data中传递参数。我得到这个问题:

File "/Users/valentin/Desktop/Identity/api/serializers.py" in create
  80.       IdentityIndividuResumeView.get_context_data(obj.id)

File "/Users/valentin/Desktop/Identity/views.py" in get_context_data
  228.         context_data = super(IdentityIndividuResumeView, self).get_context_data(**kwargs)

Exception Type: TypeError at /Api/Identification/create/
Exception Value: super(type, obj): obj must be an instance or subtype of type

编辑:

它适用于FBV model,但我想将其转换为CBV

@login_required 
def Identity_Individu_Resume(request, id) :

     personne = get_object_or_404(Individu, pk=id)

     NIU = lib.Individu_Recherche.NIUGeneratorIndividu(personne)
     personne.NumeroIdentification = NIU
     ...

以及序列化程序.py文件:

def create(self, validated_data):
    obj = Individu.objects.create(**validated_data)
    Identity_Individu_Resume(self.context.get('request'), obj.id)
    return obj

Tags: selfidobjdatagetdefcreatecontext
2条回答

序列化程序的create函数是使用类对象IdentityIndividuResumeView.get_context_data(obj.id)而不是类的实例调用的。在基于函数的视图中,传递所有必需的参数。这就是为什么它在为它工作。你知道吗

在/Users/valentin/Desktop/Identity检查您的self对象/视图.py第228行self object错误。你知道吗

我的意思是自我对象中的内容不是正确的类型。你知道吗

相关问题 更多 >