【问题标题】:Is it possible to pass values from a query to models.py in django?是否可以将查询中的值传递给 django 中的 models.py?
【发布时间】:2018-06-14 12:12:35
【问题描述】:

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

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

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

我的表格是这样的: Form

我的 models.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”在我的models.py中,字段值:“GTS do autorizado”和“Data de nascimento do autorizado”来从查询。我想将查询中的所有值复制到我的 models.py 文件中,但我不知道这是否可行。

编辑:

我的 views.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', )

在这里,我尝试获取来自帖子并位于我的 models.py 中的值,当我连接到 oracle db 时,我尝试获取来自帖子的其他值。

【问题讨论】:

  • 这里是不是故意不使用Django的ORM?
  • 我不明白这里如何涉及“模型文件”。如果你想在表单中添加数据,你可以在表单类或视图中进行。
  • 涉及模型文件,因为应该在“GTS do autorizado”和“Data de nascimento do autorizado”中写入的值在我的models.py文件中。
  • 所以你想将数据从 oracle 数据库迁移到支持 django 的数据库?
  • 我的目标是在接待员按下按钮时显示一条消息。但是正如我之前解释的那样,她在表格中输入的数据位于不同的地方。我想知道是否可以这样做。类似于我制作的版本。

标签: python django cx-oracle


【解决方案1】:

是的,有可能。

表单发布后,可以从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')

【讨论】:

  • “#从oracle DB oracle_data = some_function()获取数据是什么意思?”应该是这样的“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光标中的记录:registos.append(record)" here?
猜你喜欢
  • 1970-01-01
  • 2021-06-30
  • 2021-06-23
  • 2011-06-03
  • 2012-09-30
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多