Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django与MSSQL一起使用Pyodbc:未保存模型表单_Python_Sql Server_Django_Pyodbc_Django Pyodbc - Fatal编程技术网

Python Django与MSSQL一起使用Pyodbc:未保存模型表单

Python Django与MSSQL一起使用Pyodbc:未保存模型表单,python,sql-server,django,pyodbc,django-pyodbc,Python,Sql Server,Django,Pyodbc,Django Pyodbc,Django版本1.8.16 pyodbc版本:3.0.11b16 我一直在尝试为一个项目制作一个查看/提交表单。 我需要实现的基本目标是使用MS SQL Server 2014中的存储过程查看和编辑/保存表单中的数据。我可以在“查看”页面中使用存储过程,但无法使用编辑项和添加新项 models.py class procedures(): def view_patientsp(self, patid): cursor = connection.cursor() ret = cu

Django版本1.8.16 pyodbc版本:3.0.11b16

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

models.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)
views.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})
url.py

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

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

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

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

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

您可以使用Django的许多特性(如模板和表单)来调用存储过程。例如,在
窗体视图中,可以执行以下操作:

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)
pip install django-pyodbc-azure<1.9
不过,请小心编写弗兰肯德詹戈的代码。在过渡期间,我们努力确保两个网站可以并行运行,并在两个网站之间共享身份验证。这使我们能够随着时间的推移将特性转移到新的Django站点,同时以正确的方式在新的代码库中进行操作。在您以正确的方式使用模型、视图和模板之后,您可能会惊喜地发现,您能够以多么快的速度开始工作,尤其是使用Django丰富的现成软件包生态系统

最后一点注意:我强烈建议您为django数据库引擎使用
django pyodbc azure
(它与SQL Server或azure配合使用)。随着时间的推移,我发现它是最积极维护的,而且刚刚起作用。您可以按如下方式为Django 1.8安装它:

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)
pip install django-pyodbc-azure<1.9

pip install django pyodbc azure使用存储过程而不是django ORM有什么原因吗?Django通过ORM生成自己的SQL。在这种情况下,不建议使用存储过程。还请包括您使用的django pyodbc的版本和链接,因为有很多版本。另外,您是在Windows还是Linux上运行Django?@FlipperPA我正在使用Windows 7 64位Django Pyodbc:Version:1.0.1我正在现有Windows应用程序之上构建一个应用程序,因此使用Django。我正在使用现有的存储过程,因此windows应用程序中的任何更改也将包含在web应用程序中