0

I am actually using django only to use its Object relational mapper(ORM) feature and, currently, have nothing to do with its web-framework. At present I have successfully been able to customize django.setting to suit my database needs and defined some test data models but bumped into manage.py, which is required to build the database tables etc.

How can I call manage.py(or achieve its very purpose) from within the module where the model is defined and not from the command line?

Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
  • 3
    Just being curious, why don't you use [SQLalchemy](http://docs.sqlalchemy.org/en/rel_0_8/orm/tutorial.html) if its only an ORM you're after? – Hedde van der Heide May 04 '13 at 12:23
  • @Hedde Good point, actually while I was asking the question a colleague of mine suggested it to me. Looking into it. – Bleeding Fingers May 04 '13 at 12:33
  • @IvanKharlamov the question is, where am i supposed to plant those pieces of code? – Bleeding Fingers May 04 '13 at 22:48
  • It depends on your application's logic. :) – Ivan Kharlamov May 05 '13 at 07:28
  • @IvanKharlamov no i mean to ask should it be planted where the model is actually defined or some place else? – Bleeding Fingers May 05 '13 at 18:00
  • It could be planted anywhere in your python scripts. `models.py` is probably not the best choice, though. But, first of all, you should answer the question: **"but why the hell do I need it?"**. It looks like you really don't need django and should go with `SQLalchemy` instead. @Hedde should probably create an answer to this question which you, IMHO, should definitely accept. – Ivan Kharlamov May 05 '13 at 19:53
  • @IvanKharlamov if it can be planted anywhere how would it detect amongst 10-20 modules/packages which ones contains has a `model` defined? To answer your other concern, it is highly unlikely that I'll be using it. But that doesn't answer the question. Or does it? – Bleeding Fingers May 05 '13 at 20:17
  • Django project has a [predefined structure](https://docs.djangoproject.com/en/1.5/intro/tutorial01/), so that syncdb in the scope of the project will affect only the apps which are a part of `INSTALLED_APPS` in the `settings.py` file. – Ivan Kharlamov May 05 '13 at 20:25
  • @IvanKharlamov i suggest you combine your 1st and last comment into answer which I will accept because it worked. Hedde's comment was a really apt suggestion but not an answer – Bleeding Fingers May 06 '13 at 18:42

1 Answers1

1

Well, Hedde is probably right about SQLAlchemy being a better suit, but if you still want to programmatically access manage.py commands, checkout answers to this question.

Django project has a predefined structure, so that syncdb in the scope of the project will affect only the apps which are a part of INSTALLED_APPS in the settings.py file.

The really fun stuff is that you can even have on-the-fly schema alteration with django-mutant. Of course, you should use it only when you're absolutely sure about when and what you're doing :))).

Community
  • 1
  • 1
Ivan Kharlamov
  • 1,889
  • 2
  • 24
  • 33