Django allows you to split your project in applications, and Django
applications are quite linked to models. But you can create your own
libraries, this is Python. As an example, we create applications to
connect to different web services and we use Django in the project.
Each web service mapping is in its own Python packages. The Django
urls and views just uses that package.
Hope it helps!
--
Antoni Aloy López
Blog: http://trespams.com
Site: http://apsl.net
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
I hope some dguru answers this because it is important.
I reckon business rules should be in models.py because that represents
the database and I've always thought that is where the business rules
belong for the sake of data integrity.
There is probably nothing wrong with multiple model files named for the
tables in the app if you want to keep them small. You might want to
import them 'from app import table_x.table_x as table_x'. Bit ugly I
suppose - this is really why it makes lotsa sense to split a big project up.
Business logic is different than business rules. That is simply the
application logic and that probably belongs in views.py but there is
nothing stopping you importing stuff from app_logic.py which can import
stuff from big_project.py.
If it is a giant project you probably ought to split it into smaller
apps each of which is focused on a self-contained subset of your entire
functionality. That way, you might be able to put parts of it on
sourceforge and I can use it :)
Mike
>
> Thanks for your time.
>
Stuffing logic and huge amounts of code into models.py sounds like a
terrible idea. It doesn`t belong in views.py either. In a simple app
this works fine, but not for larger projects. There might be at least
two different ways to handle this; 1) like one guy points out - it`s
up to the developer to choose or 2) try to get the django
community/core dev/etc to come up with a more standard way of doing
it. I`m probably more in support of the second option.
Almost all of my projects start quite small, but outgrown one
models.py and one views.py rather quick. And that`s not even thinking
of all the business logic, just plain data models and views. So what
do I do?
Thanks for all your input anyway :-)
Ok, I see your point, but still - there`s nothing about this in the
main django documentation as far as I know. The docs should have a
section about organizing projects where the standard models.py and
views.py doesn`t fit anymore.
--
Mvh/Best regards,
Thomas Weholt
http://www.weholt.org
I would also like the Django guru's to come up with a best practices page.
Just ideas of how you could structure code for this or that kind of project.
You certainly can't cover all bases but at least it could serve as a starting point.
On a related note, there is a thread about the SITE_ID.
I can see how multiple sites can be managed from a single project but 1 project
means 1 settings.py file so it's not clear to me how you can then use the SITE_ID.
Anyway, as for structuring my code, I always end up splitting my models and views
and as Bruno pointed out, using __init__.py isn't hackish but takes a bit getting used too.
It's actually a nice Python feature.
Dealing with a few smaller files is in my opinion more logical, cleaner and makes
it faster to solve problems if they arise.
Putting your business logic in your models is the preferred way it seems.
If you have other code like utilities that aren't bound to http or django specific,
i would put them in a another directory, make it package and install it so
you can use it in any code/project.
I have a big project that i'm working on and i'm putting all application related code,
media and templates included, in the application directory.
To me, it makes more sense to do it like this than to have a single template directory
with sub directories for every application.
This ties the apps into the project and i want to avoid this is possible so reusing
the applications should be easier.
Regards,
Benedict
It's not there as it's a plain Python feature. Please read about it on
Python site here - http://docs.python.org/tutorial/modules.html#packages.
I've no idea if there are links to Python docs in the first djangobook
chapter or on the Django's Introduction page.
If such feature should be explained on Django docs, every other Python
feature should be there as well.
We come to this pretty often in MooTools community - it has to clearly
be said that framework is just extending the language.
Regards
Piotr Zalewa
--
blog http://piotr.zalewa.info
jobs http://webdev.zalewa.info
twit http://twitter.com/zalun
face http://facebook.com/zaloon
[Thinks: definitely time for a "Python for Djangonauts" class].
regards
Steve
--
DjangoCon US 2010 September 7-9 http://djangocon.us/
I`m asking for a best-practice advice, and have gotten plenty of good
examples of ways to solve this "problem", but I still think this
should be covered by the standard django docs anyhow, especially since
there are some django specific magic related to the files generated by
using manage.py startapp. It`s obviously not clear to all django users
how to best handle growing projects, split into modules etc and just
taking some of the info in this thread, re-writing it and giving a few
examples and then adding it to the std docs would be enough.
I section under "The model layer -> Advanced" called "Organizing
models and code in larger projects". Details on how to splitting
models over several files, if and how you should seperate logic from
models etc.
>> especially since
>> there are some django specific magic related to the files generated by
>> using manage.py startapp.
>
> ??? Which "django specific magic" ???
I was referring to syncdb. As far as I know, models defined outside
any models.py or models module won`t be picked up by syncdb, but I
might be wrong.
>
>> It`s obviously not clear to all django users
>> how to best handle growing projects, split into modules etc
>
> I can only second Steve on this : time for "Python for Djangonauts"
> class then. FWIW, I'm amazed at how many persons seem to pick up
> django without any background in Python.
Hmmm ... do you or Steven know anything about my programming
background? My googling and even answers on this thread seem to
indicate that I`m not alone pondering about how to organize my django
code. I know how to structure my code in modules, but for
re-usability`s sake for instance it would be nice to have some
guidelines on how to structure your code in larger django projects and
it would be nice to have those guidelines in the otherwise excellent
django docs.
>
>> and just
>> taking some of the info in this thread, re-writing it and giving a few
>> examples and then adding it to the std docs would be enough.
>
> Mmm... Not sure this would help. A pointer to Python's doc and a
> strong emphasis on the fact that you definitly have to learn Python to
> go any further would be more helpful in the long run IMHO. But well,
> if you think you can contribute something useful, please go ahead and
> submit your patch to the team - it's an OSS project, isn't it ?
I`m trying to contribute to the django community by writing code,
reusable apps, being active on gropus like this etc. I could even try
to help out writing docs, but if my questions are going to be answered
by people who question my competence that wouldn`t be very motivating,
would it?
I`ll take all the hints and advice given in this thread and try to use
it to organize my ongoing project.Thanks for your time, everyone.
Regards,
Thomas
>>
>>> It`s obviously not clear to all django users
>>> how to best handle growing projects, split into modules etc
>>
>> I can only second Steve on this : time for "Python for Djangonauts"
>> class then. FWIW, I'm amazed at how many persons seem to pick up
>> django without any background in Python.
>
> Hmmm ... do you or Steven know anything about my programming
> background? My googling and even answers on this thread seem to
> indicate that I`m not alone pondering about how to organize my django
> code. I know how to structure my code in modules, but for
> re-usability`s sake for instance it would be nice to have some
> guidelines on how to structure your code in larger django projects and
> it would be nice to have those guidelines in the otherwise excellent
> django docs.
>
Hold hard, there! That's Steve, not Steven, Thomas. And I was musing
about it being an opportunity to write and promote a new training class,
rather than specifically suggesting that you personally needed training.
>>
>>> and just
>>> taking some of the info in this thread, re-writing it and giving a few
>>> examples and then adding it to the std docs would be enough.
>>
>> Mmm... Not sure this would help. A pointer to Python's doc and a
>> strong emphasis on the fact that you definitly have to learn Python to
>> go any further would be more helpful in the long run IMHO. But well,
>> if you think you can contribute something useful, please go ahead and
>> submit your patch to the team - it's an OSS project, isn't it ?
>
> I`m trying to contribute to the django community by writing code,
> reusable apps, being active on gropus like this etc. I could even try
> to help out writing docs, but if my questions are going to be answered
> by people who question my competence that wouldn`t be very motivating,
> would it?
>
I don't think anyone's questioning your competence, and your desire to
help in a community-spirited way is very welcome. But you should realize
(and probably will if you think about it) that many people who do ask
questions on this list could use better Python knowledge than they
currently have. If you aren't in that position then fine.
> I`ll take all the hints and advice given in this thread and try to use
> it to organize my ongoing project. Thanks for your time, everyone.
>
You are correct in saying that the Django docs are excellent, but
there's only so much they can do.
If you do come up with useful guidelines I am sure that nobody is going
to complain if you submit a patch to the docs. One of the issues is that
size is not the only metric of complexity, and it's difficult to frame
generic advice about the organization of large projects.
I'm surprised no one mentioned manager classes yet. This is an example for
models.py:
from django.db import models
from yourapp.managers import SomeObjectManager
class SomeObject(models.Model):
objects = SomeObjectManager()
Now you can do things like: SomeObject.objects.foo() which is defined in your
manager class. This allows a much better separation of logic and models.
> Should there be a better and standardized way to organize huge
> projects in django?
Yes. Making multiple applications. I highly recommend watching this DjangoCon
video by James Bennett: http://www.youtube.com/watch?v=A-S0tqpPga4
It explains how apps can be made reusable. For example a "blog" application is
actually not 1 monolythic application, but 8+ tiny apps. User registration and
account management can also be written as 4+ little apps.
SInce watching that video, my projects are composed by default of 2 apps. A
"projectname" for the core backend stuff, and a "projectname-site" with the
templates, settings, and frontend media. This is imho a nicer base to start
"hooking in" more applications.
More functionality (tagging, rating, accounts, etc.. will all become separate
apps) which will be hooked in the "projectname-site" project. In case you
wonder, http://www.djangopackages.com/ gives you quite a wealth of small
reusable parts that can be hooked in directly.
Greetings,
Diederik
> SInce watching that video, my projects are composed by default of 2 apps. A
> "projectname" for the core backend stuff, and a "projectname-site" with the
> templates, settings, and frontend media. This is imho a nicer base to start
> "hooking in" more applications.
Can you give an example of the second application where you put the templates,
settings and so on?
I'm trying to put all the media, templates in the application directory.
However, it's not clear to me how i can do this as you end up with 2 media url's.
One for the project and one for the applications, same goes for the templates.
Regards,
Benedict
I agree with Thomas that at least some guidelines would be nice.
As what you and Steve have been saying about the need for a "Python for Djangonauts" class,
i think it would be a good idea also.
I'm wondering though what specific Python topics you would touch?
One that comes to mind is the use __init__,py and this modules in general as this
knowledge is needed to understand how you can split your models and views.
This would be what i would put in the guidelines for code structuring.
Topics i think would be interesting:
1. Project vs App
- What is a project? When do you use applications?
- Possible examples of coding structure
- 1 site in 1 project
- many sites in 1 project
- ...
2. Splitting models & views
3. How can you cleanly seperate an application further from a project
4. Building reusable applications
5. Advanced building methods
- Buildout
- Virtualenv
- ...
If we could agree on a list of topics, we can start writing it ourselves.
We have people here with lots of experience regarding these kind of
more advanced topics.
I wouldn't mind to write something on point 2 but that would need
to be verified by someone with loads more experience than me before
it could be published.
Regards,
Benedict
I`ll upload it to the cheeseshop as soon as I can get time to
debug/test a bit more. The code isn`t pretty at the moment, but it
seems to get the job done.