Creating A Model For Existing DB Table

650 views
Skip to first unread message

octopusgrabbus

unread,
Nov 10, 2010, 12:23:55 PM11/10/10
to Django users
I have an existing MySQL table. It's fairly straightforward:

part_num char(20)
ept_type smallint (default 0) not null
inv_id integer (default 0) not null
load_date date

columns 2 and 3 (ept_type and inv_id) form a unique key. An example of
a unique key is 43 and 80581770.

I've tried to create a model

from django.db import models

# Create your models here.
class ept_inv(models.Model):
part_num = models.CharField(max_length=20)
ept_type = models.SmallIntegerField
inv_id = models.IntegerField
load_date = models.DateField

but am getting errors on syncdb
class ept_inv(models.Model):
File "/usr/local/lib/python2.6/site-packages/django/db/models/
base.py", line 9
2, in __new__
new_class.add_to_class(obj_name, obj)
File "/usr/local/lib/python2.6/site-packages/django/db/models/
base.py", line 2
12, in add_to_class
value.contribute_to_class(cls, name)
TypeError: Error when calling the metaclass bases
unbound method contribute_to_class() must be called with
SmallIntegerField i
nstance as first argument (got ModelBase instance instead)

What should I be doing?
Thank you.

Daniel Roseman

unread,
Nov 10, 2010, 12:31:19 PM11/10/10
to Django users
All your fields need to be instances, not classes - eg
SmallIntegerField() not SmallIntegerField.

Also note that Django has built-in functionality for building a model
from an existing table: just run ./manage.py inspectdb
--
DR.

octopusgrabbus

unread,
Nov 10, 2010, 12:36:53 PM11/10/10
to Django users
Thanks.

Sandip Bhattacharya

unread,
Nov 10, 2010, 12:38:48 PM11/10/10
to django...@googlegroups.com
On Wed, Nov 10, 2010 at 09:23:55AM -0800, octopusgrabbus wrote:
> # Create your models here.
> class ept_inv(models.Model):
> part_num = models.CharField(max_length=20)
> ept_type = models.SmallIntegerField
> inv_id = models.IntegerField
> load_date = models.DateField

Call the model classes. Dont return their references.


class ept_inv(models.Model):
part_num = models.CharField(max_length=20)

ept_type = models.SmallIntegerField()
inv_id = models.IntegerField()
load_date = models.DateField()

- Sandip


Simone Dalla

unread,
Nov 10, 2010, 1:30:28 PM11/10/10
to django...@googlegroups.com


2010/11/10 Sandip Bhattacharya <san...@foss-community.com>
python manage.py inspectdb > models.py



--
Simo

- Registered Linux User #395060

- Software is like sex, it is better when it is free  --> Linus B. Torvalds

octopusgrabbus

unread,
Nov 10, 2010, 3:49:00 PM11/10/10
to Django users
Thanks, and I took this one step further. I have a test application
directory, and ran the

python manage.py inspectdb

into a special models.py file that I'll use for future reference.

On Nov 10, 1:30 pm, Simone Dalla <simoda...@gmail.com> wrote:
> 2010/11/10 Sandip Bhattacharya <sand...@foss-community.com>

Michael

unread,
Nov 11, 2010, 9:47:30 AM11/11/10
to Django users
no missing parenthesis ?

class ept_inv(models.Model):
part_num = models.CharField(max_length=20)
ept_type = models.SmallIntegerField()
inv_id = models.IntegerField()
load_date = models.DateField()

octopusgrabbus

unread,
Nov 12, 2010, 9:31:02 AM11/12/10
to Django users
Thanks. That was one of the problems.

veeravendhan

unread,
Nov 19, 2010, 1:48:06 AM11/19/10
to Django users
Why do you want to create a models, better go with "inspectdb" will
give you the models.
check out this: http://docs.djangoproject.com/en/dev/howto/legacy-databases/?from=olddocs

-Veera
Reply all
Reply to author
Forward
0 new messages