from odbc to table

18 views
Skip to first unread message

Nebros

unread,
Nov 15, 2012, 4:02:03 AM11/15/12
to django...@googlegroups.com
good morning.
i have an odbc connect to a mssql db... it works and it looks like this:
views-----------------------------------
def kundendaten(request):
    cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=MAURITIUS;DATABASE=baan5c;UID=***;PWD=*****')
    cursor = cnxn.cursor()
    cursor.execute("SELECT x.t_name, y.t_mail FROM tttaad200000 as x, tttcmf200000 as y WHERE (x.t_name = y.t_name)")
    row = cursor.fetchall()
    return render_to_response("kundendaten.html", { 'row': row }, context_instance=RequestContext(request))
-------------------------------------------
 
what does it do? it gives out the name and the e-mail of a customer. the values are from two tables together and with "WHERE (x.t_name = y.t_name)" i avoid redundancy.
 
here the output of these request:
-------------------------------------------
[('Ackermann Marcel (THA) ', 'marcel.***@***.ch '), ('Adami Renate (HEI) ', 'renate.***@***.ch '), ('Ammann Cornelia (HEI) ', 'cornelia.***@***.ch '), ('Ausbildung A. Schwaller (HEI) ', 'giulia.***@***.ch '), ('General_User_NT (SYS) ', 'baan-alarm@***.ch '), ('Benz Roger (THA) ', 'roger.***@***.ch '), ('Berktold Stefan (THA) ', 'stefan.***@***.ch '), ('Biegger Christian (THA) ')]
-------------------------------------------
 
and now my problem... i dont know how that i can give it out into a table. i createt a modul:
modul-----------------------------------
from django.db import models
class Person(models.Model):
    t_name = models.CharField(max_length=30)
    t_mail = models.CharField(max_length=30)
--------------------------------------------
 
i dont know whether i need this and i dont know how to use the table, that it works...
can anyone help me pls. (if its possible pls not just text how to do, im very new and i can learn more when i see a correct code)

Tom Evans

unread,
Nov 15, 2012, 5:49:31 AM11/15/12
to django...@googlegroups.com
Normally in Django you specify the models in code, and then ask Django
to create the corresponding tables. If you are using a legacy database
- one that already exists - then you will need to do more work.

You will need to tell Django what table name each model corresponds
to. There is some excellent documentation on using django with an
existing legacy database.

https://docs.djangoproject.com/en/1.4/howto/legacy-databases/

Also, I do not understand why you are creating and specifying the
database connection in the view. If you want to use Django models, you
must configure your database with Django. This is complicated by the
fact you want to use an unsupported backend. There is a 3rd party
Django ODBC backend:

http://code.google.com/p/django-pyodbc/

and instructions on how to get support for those backends:

https://docs.djangoproject.com/en/1.4/ref/databases/#using-a-3rd-party-database-backend

Cheers

Tom

Nebros

unread,
Nov 15, 2012, 9:36:10 AM11/15/12
to django...@googlegroups.com, teva...@googlemail.com
I have tryed this way before, but it doesent work. it has many fails in the code... one of these are: "AttributeError: 'Settings' object has no attribute 'DATABASE_COLLATE'"
thats why i searched another way... if you can help me to make this run, i will use this. but i asked many times for that and no one gave answer... ^^
Reply all
Reply to author
Forward
0 new messages