【问题标题】:I can't get the content of an object我无法获取对象的内容
【发布时间】:2022-01-13 21:07:24
【问题描述】:

我必须发布一些对象的内容,但我无法从这些对象中获取这些内容。 任何帮助将不胜感激。

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:/')

【问题讨论】:

    标签: python-3.x django django-models


    【解决方案1】:

    从 django 模型中获取数据

    所以模型只是数据库上的 ORM(对象关系映射)。

    主要使用 SQL,更准确地说是使用 Postgresql 数据库。

    就像在 SQL 中一样,当你想要获取数据时,你必须提供行的 id 或一些字段值来获取行。在这里,您还必须提供 id 或一些字段值来获取行。

    如果您有之前通过模型保存在数据库中的对象的 ID。 然后您可以通过以下方式获取模型实例,即表中的数据行: instance = models.Cadastro_alunos.objects.get(id=1)。 如果您需要的行是 id 1。对于任何其他字段也是如此。如果查询的 id 不存在于 db 中,则会报错。

    更多详情请参考文档:https://docs.djangoproject.com/en/3.2/topics/db/models/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      相关资源
      最近更新 更多