gpt4 book ai didi

python - Django 与 MSSQL 使用 Pyodbc : Model Forms not being saved

转载 作者:太空宇宙 更新时间:2023-11-04 05:18:25 25 4
gpt4 key购买 nike

Django 版本 1.8.16pyodbc版本:3.0.11b16

我一直在尝试为一个项目制作一个查看/提交表单。我需要实现的基本目标是在 MS SQL Server 2014 中使用存储过程查看和编辑/保存表单中的数据。我能够在 View 页面中使用存储过程但无法通过编辑进行项目并添加一个新项目。

模型.py

class procedures():
def view_patientsp(self, patid):
cursor = connection.cursor()
ret = cursor.execute("EXEC PR_PRES_viewpatient @uid=? ", (patid))
cursor.close()
return ret


class Patient(models.Model):
patientid = models.AutoField(db_column='PatientID', primary_key=True)
pyear = models.DecimalField(db_column='Pyear', max_digits=10, decimal_places=0, blank=True, null=True)
dref = models.DecimalField(db_column='DRef', max_digits=10, decimal_places=0, blank=True, null=True)
title = models.TextField(db_column='Title', blank=True, null=True)
fname = models.TextField(db_column='FName', blank=True, null=True)
lname = models.TextField(db_column='LName', blank=True, null=True)
dob = models.DateTimeField(db_column='DOB', blank=True, null=True)
pamonth = models.TextField(db_column='PAMonth', blank=True, null=True)
payear = models.TextField(db_column='PAYear', blank=True, null=True)
padays = models.TextField(db_column='PADays', blank=True, null=True)
sex = models.TextField(db_column='Sex', blank=True, null=True)

View .py

def view_patient(request):
if request.method == 'POST':
form = viewpatientform(request.POST)
return render(request, 'lis/view.html', {'form': form})
else:
form = viewpatientform()
if form.is_valid():
procedure = procedures()
ret = procedure.view_patientsp(request.POST['fields'])
return render_to_response('lis/view.html', {'form': form})

网址.py

urlpatterns = [
url(r'^$', views.pat_list, name='index'),
url(r'^view/(?P<patid>\d+/)$', views.view_patient, name='viewpatient'), ]

view.html

{% block body %}
{% load materializecss %}
{{ form|materializecss }}
<button type="submit" class="btn btn-primary">Submit</button>
{% endblock %}

最佳答案

您有几个选择。我将尝试在这里概述它们,因为我有一个类似的项目,我们正在使用另一种语言编写的非常大的站点,并由带有存储过程的 SQL Server 支持,我们正在缓慢但肯定地迁移到 Django。

我们的目标是随着时间的推移进行全面重写。这意味着用 Django 的模型和 View 替换存储过程中的逻辑,因为 ORM 非常强大。我们发现它减少了重复并显着提高了可重用性,从而减少了代码总量。

您可以使用 Django 的 inspectdb 等功能来生成一组初始 Django 模型以与您的数据库进行交互。它需要一些改进,但这使我们能够使用一些功能(如 Django 的管理)对旧数据库进行 crud 操作,而不是编写三层代码来执行简单的查找表更新。我不建议自定义管理,但将它用于它擅长的地方是开始熟悉 Django 做事方式的好方法。

您可以使用 Django 的许多功能(例如模板和表单)来调用存储过程。例如,在 FormView 中,您可以这样做:

class MyFormView(FormView):
template_name = 'home.html'
form_class = MyForm
success_url = '/hooray/'

def form_valid(self, form):
sender = form.cleaned_data['sender']
message = form.cleaned_data['message']

cursor = connections['default'].cursor()
cursor.execute('EXEC usp_insert_message @sender = ?, message = ?', sender, message)

return super(MyFormView, self).form_valid(form)

但是,请谨慎编写 FrankenDjango。我们在过渡期间努力确保两个网站可以并行运行,并在两者之间共享身份验证。这使我们能够随着时间的推移将功能转移到新的 Django 站点,同时在新代码库中以正确的方式做事。在您以正确的方式开始使用模型、 View 和模板之后,您可能会惊喜地发现您可以如此快速地开始移动,尤其是使用 Django 丰富的现成包生态系统。

最后一点:我强烈建议为您的 Django 数据库引擎使用 django-pyodbc-azure(它适用于 SQL Server Azure)。随着时间的推移,我发现它是最积极维护的,并且刚刚工作。您可以像这样为 Django 1.8 安装它:

pip install django-pyodbc-azure<1.9

可在此处找到更多详细信息:https://github.com/michiya/django-pyodbc-azure

祝你好运!

关于python - Django 与 MSSQL 使用 Pyodbc : Model Forms not being saved,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41125936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com