我正在开发我的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
序列化程序的
create
函数是使用类对象IdentityIndividuResumeView.get_context_data(obj.id)
而不是类的实例调用的。在基于函数的视图中,传递所有必需的参数。这就是为什么它在为它工作。你知道吗在/Users/valentin/Desktop/Identity检查您的self对象/视图.py第228行self object错误。你知道吗
我的意思是自我对象中的内容不是正确的类型。你知道吗
相关问题 更多 >
编程相关推荐