0

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.

6
  • do python manage.py syncdb Commented Jul 1, 2014 at 8:35
  • 1
    No it's not ok. Database's logic should be located in models - MVC pattern Commented Jul 1, 2014 at 8:37
  • but my co-team refused to create table structure in models.py and instead create database tables directly at postgre...i said that we have to follow what's in django documentation..can you tell me what would be our problems if we didnt define table in models.py? Commented Jul 1, 2014 at 8:40
  • If you are not going to use built-in Django ORM features - it's kind of ok. Otherwise you have to declare your db structure in models.py. Also, you'd better have really solid argument for not using built-in ORM features (e.g. "we're going to use SQLAlchemy" or "ORMs sucks" :)) Commented Jul 1, 2014 at 8:40
  • 1
    Looks like you are confusing "code-first" approach with using ORM at all. If you have existing db, you can still use Django ORM by going "db first". there's an article in django docs on how to integrate django with legacy (read: pre-existing) database. So just let them have their database structure declared where they want it to be, and adapt :) Commented Jul 1, 2014 at 8:42

1 Answer 1

1

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...
Sign up to request clarification or add additional context in comments.

2 Comments

Just a little suggestion - you should never write sql in views. Move it to models or to "application logic" layer. By accessing database in views you essentially tie your views to database, which greatly increases coupling and could potentially lead to problems in the future (e.g. migrating to another DB engine, introducing some common db-related functionality, etc.). In my opinion, custom SQL is a perfect use case for custom model manager.
aye. I just kept dreaming that code.djangoproject.com/ticket/21181 will happen soon and never bothered to rework temporary solution into more permanent one :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.