是否可以将查询中的值传递给型号.py在Django?

2024-10-06 11:18:16 发布

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

我正在尝试制作一个应用程序,在其中一个人插入一个id和一个人的生日日期,并输出一些东西。我的 这里的疑问是:有一个带有多个id和生日日期的查询(来自oracle),使用:

connection = cx_Oracle.connect(...) 
cursor = connection.cursor()
cursor.execute("query")

我的问题是:是否可以将查询中的一些值传递给我的型号.py文件?例如,在我的型号.py文件我有一个名为“id”的字段,我想将字段“doente”中查询的所有记录传递给型号.py文件名为“id”。你知道吗

我的表格是这样的: Form

还有我的型号.py文件是:

class PatientTutor(models.Model):
    id = models.AutoField(primary_key=True)
    username = models.CharField(max_length = 8)
    patientID = models.CharField(max_length = 8)
    created = models.DateTimeField(auto_now_add = True)
    birthday = models.DateField(blank=True, default=None)
    birthdayPatient = models.DateField(blank=True, default=None)

其中:“GTS do autorizante”和“Data de nascimento do autorizante”字段的值在我的型号.py字段中的值:“GTS do autorizado”和“Data de nascimento do autorizado”来自查询。我想将查询中的所有值复制到我的型号.py但我不知道这是否可行。你知道吗

编辑:

我的视图.py文件如下所示:

def mostraFormTutorPaciente (request):
return render(request, 'CartaoCliente/mostraFormTutorPaciente.html')

def TutorPaciente (request):
if request.method == 'POST':

    AutorizanteGTS = PatientTutor.objects.get(id = request.POST['AutorizanteGts'])
    NascAutorizante = PatientTutor.objects.get(birthday = request.POST['DtNascAutorizante'])

    connection = cx_Oracle.connect("....")

    cursor = connection.cursor()
    cursor.execute("select t_doente, doente, nome, dt_nasc, telef1, telef2" + \
            "from gh.sd_doente where doente<>'" + patientID  + \
            "'and nvl(flag_anulado, 'N')<>'S' and dt_nasc >= sysdate-18*365 and (trim(telef1) in (" + inClause + ") or trim(telef2) in (" + inClause + "))")

    registos =[]
    for record in cursor:
        registos.append(record)

    return render(request, 'CartaoCliente/TutorPaciente.html', )

在这里,我试图得到来自帖子的值,这些值在我的文档中型号.py当我连接到oracledb时,我试图获取来自post的其他值。你知道吗


Tags: and文件inpyidtruemodelsrequest
1条回答
网友
1楼 · 发布于 2024-10-06 11:18:16

是的,这是可能的。你知道吗

发布表单时,可以从oracle数据库中获取数据,然后将其保存到模型对象中。你知道吗

使用此示例视图查看其工作原理:

def my_view(request):
    if request.method == 'POST':
    form = MyForm(request.POST)

    if form.is_valid():
        # get the data from the oracle DB
        oracle_data = some_function()

        # create a model object that saves the data
        obj = MyObject.objects.create(
              birthday=oracle_data['birthdate']
              some_other_field=some_value
              )

        # everything worked out, return a response
        return render(request, 'some-template.html')

    # for data was not valid, return an error
    return render(request, 'error-template.html')

相关问题 更多 >