user3204806
user3204806

Reputation: 322

Database Tables not defined in DJANGO models

Is it fine not to define table stucture in models.py...I mean we are using

cursor.execute('some query')

so its okay not to make something like this :

def tblNew(models.Model) col1 = models.CharField(max_length=2)

Thanks for guidance, Im a newbie in django.

Upvotes: 0

Views: 1173

Answers (1)

Odif Yltsaeb
Odif Yltsaeb

Reputation: 5656

You CAN create database tables directly in postgresql But you'd have to create models.py files later because otherwise you will be missing out on several things. If you have pre-existing database, then you have django-admin.py command to pick them up for you (https://docs.djangoproject.com/en/dev/ref/django-admin/#inspectdb)

  • ORM. It makes alot of things easyer. Writing sql in views? Why would that be good? Because that is what you get if you do not use ORM at all. And while i do have sql in some of my views, it only exists there because django lacks some features like language collation specific sorting. But otherwise writing sql in views hampers readability.
  • You can't use database migrations. South (http://south.aeracode.org/) is one awesome project that most big django projects use. In fact i think it was most used django project in some apps poll. Django 1.7 will provide the migrations too, but you will need to declare models im models.py for that. What you loose if you do not use database migrations - SANITY. I do not exaggerate here. Keeping track of database changes in development and prelive/live servers is nightmare otherwise.
  • Also is not using django ORM in some places (not declaring models) kind of strange, while you will definately use django models and ORM elsewhere - or will you stop using django authentication, sessions and site logic also? All those use django ORM...

Upvotes: 1

Related Questions