I can't get the content of an object
I have to POST the contents of some objects, but I'm not able getting this content from the objects. Any help would be greatly appreciated.
from django.shortcuts import render, redirect
from cadastro.forms import CadastroForm
import requests
from . import models
def cadastro_usuario(request):
data = {}
data['form'] = CadastroForm()
return render(request, 'cadastro.html', data)
def create(request):
form = CadastroForm(request.POST or None)
user_name = models.Cadastro_alunos.user_name
user_cpf = models.Cadastro_alunos.user_cpf
user_password = models.Cadastro_alunos.user_password
user_genre = models.Cadastro_alunos.user_genre
user_email = models.Cadastro_alunos.user_email
dados_obrigatorios = {'token': 'xxxxxxxxxxxxxxxxxxxx',
'course_id': '1',
'course_plan': '1',
'user_name': user_name,
'user_cpf': user_cpf,
'course_type': '4',
'user_email': user_email,
'user_password': user_password,
'user_genre': user_genre}
print(dados_obrigatorios)
try:
form.save()
y = requests.post('https://www.iped.com.br/api/user/set-registration', data=dados_obrigatorios)
print(y.json())
except Exception as e:
print(e)
return redirect('https:/')
Fetching data from django models
So model is just an ORM(object relational mapping) over the database.
Mostly SQL is used and more precisely Postgresql database is used.
Like in SQL when you want to fetch data, you have to provide id of the row or some field value to fetch the row. Here you also have to provide the id or some field value to get the row(s).
Like if you have the id of the object that you saved previously through your model in the db.
Then you can fetch the model instance ie the data row from table by:
instance = models.Cadastro_alunos.objects.get(id=1)
.
If the row you need is of id 1. Similarly is done for any other field. If queried id not present in db then it will give an error.
For more details please refer to the documentation: https://docs.djangoproject.com/en/3.2/topics/db/models/