Django API Rest : Fonction non prise en compte
Bonjour tout le monde,
J'ai un petit soucis avec mon application Django et la mise en place des fonctions API Rest.
J'ai mis en place différentes fonctions :
- Création
- Affichage liste objets
- Affichage en détail d'un objet
- Édition d'un objet
Néanmoins, ma fonction de création ne va pas jusqu'au bout et j'avoue débuter avec l'API Django et avoir du mal à comprendre certains principes.
Dans mon fichier serializers.py :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| 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)
Identity_Individu_Resume(self.context.get('request'), obj.id)
return obj |
Et mon fichier views.py :
Code:
1 2 3 4
|
class IndividuCreateAPIView(CreateAPIView) :
queryset = Individu.objects.all()
serializer_class = IndividuCreateSerializer |
Et mon urls.py :
Code:
url(r'^create/$', IndividuCreateAPIView.as_view() , name="Create"),
Lorsque je me connecte sur mon application et que j'arrive sur l'API, tout fonctionne parfaitement.
Mais lorsque j'exécute les mêmes commandes via un fichier .py dans mon terminal :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
import requests
url = 'http://localhost:8000/Api/Identification/create/'
filename1 = '/Users/valentin/Desktop/Django/DatasystemsCORE/Media/pictures/photo.jpg'
filename2 = '/Users/valentin/Desktop/Django/DatasystemsCORE/Media/Carte_Identite/carte_ID.gif'
files = {'Image' : open(filename1,'rb'), 'CarteIdentite': open(filename2,'rb')}
data = {
"Etat": "Vivant",
"Civilite": "Monsieur",
"Nom": "creation",
"Prenom": "via-api",
"Sexe": "Masculin",
"Statut": "Célibataire",
"DateNaissance": "1991-11-23",
"VilleNaissance": "STRASBOURG",
"PaysNaissance": "FR",
"Nationalite1": "FRANCAISE",
"Nationalite2": "",
"Profession": "JJJ",
"Adresse": "12, rue des fleurs",
"Ville": "STRASBOURG",
"Zip": 67000,
"Pays": "FR",
"Mail": "",
"Telephone": ""
}
response = requests.post(url, files=files, data=data)
print(response.text) |
Et bien j'ai mon objet qui est créé mais ma fonction create n'est pas exécuté :
Code:
1 2 3 4
| def create(self, validated_data):
obj = Individu.objects.create(**validated_data)
Identity_Individu_Resume(self.context.get('request'), obj.id)
return obj |
Pourquoi donc ? :/