Questions on project vs app, and how they relate

36 views
Skip to first unread message

memilanuk

unread,
Nov 19, 2015, 11:17:27 PM11/19/15
to django...@googlegroups.com
So... if I'm understanding things correctly, a Django 'project' can have
multiple 'apps' in it, and these 'apps' can be reused to some extent
between projects. As in: copy the folder of code for an 'app' from one
project to another (editing things as needed for the new project)?

An app can either have its own style/templates, or it can use the
'project' templates / CSS, correct?

Maybe I haven't came across it yet... but how do you tie the different
apps of a project together? How does the

As an example... lets say I want to set up a web site for running sports
tournaments. One 'app' might be the competitor information, sort of
like a contact / address book. Another might be for setting up specific
events, and another would be for entering the scores and doing various
things with them (validating, enforcing bounds / constraints,
calculating aggregates, breaking ties, publishing results). The
specific functions are distinct enough it seems like they would each be
separate apps, but they all need to access the same database, possibly
the same tables to get the information that one app or another needs.
This seems to be where my (admittedly very basic) understanding of how
Django works pretty much falls apart...

Thanks,

Monte

Muhammad M

unread,
Nov 19, 2015, 11:33:06 PM11/19/15
to django...@googlegroups.com
Dear Monte,

Don't consider me an expert but here is how you may want to handle this scenario. 

1. django-admin.py startproject tournaments 

Now, cd into the 'tournaments' directory (wherein you have a manage.py file). From that directory you can create as many different apps (actually "components") of your tournaments project.

So, you do:
python manage.py startapp competitors 
python manage.py startapp eventshandler
python manage.py startapp scoreboard

Each of these apps will handle each set of data and activities you outlined above.
Each app may also have its own set of templates (e.g. competitors/templates/), static assets for JS, CSS, etc. (e.g. competitors/static) and, of course the necessary models, views, urls, admin, etc. modules as needed.

This might be the best way to approach it.

However, you may also choose to share put all your templates or static assets in one app and have all other apps fetch them from there. For example, you may put all your CSS and JS files in competitors/static/ . 

The separation of code and content into various apps is simply to help you better organize your project in a way that is simple and makes sense to you. But if your preference is different for a particular project or something else makes sense to you, you may fee free to do so as long as you know what you are doing and done break your code along the way.

If you haven't already done so, I would advise that you go over the official Django tutorial at https://docs.djangoproject.com/en/1.9/intro/ . While working through it just remember that you can do with/to each of your apps what you did to the "polls" app in that tutorial.

I hope this helps in some way. 

All the best. :)

Sincerely,
Muhammad 



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/n2m6qk%249u3%241%40ger.gmane.org.
For more options, visit https://groups.google.com/d/optout.

memilanuk

unread,
Nov 20, 2015, 12:31:54 AM11/20/15
to django...@googlegroups.com
Hello Muhammad,

Yes that does help, thank you very much!

I'm still a little unclear on sharing data between the apps. If it was
a standalone SQL database (not using Django or an ORM), I'd think it
would be appropriate to set up one database, with various tables, shared
between all three aspects of the project.

Example: Besides their contact info, competitors would have other info
specific to each person - their classification(s) in various equipment
categories, etc. The EventHandler would need the competitor information
for combining/splitting relays, etc. The Scoreboard would need
information from both, to tie the scores to a specific competitor at a
given event.

The way it looks to me, all the model info (models.py) is particular to
each specific app. How would you get them to all share *one* database
for the project, using the Django ORM?

Muhammad M

unread,
Nov 20, 2015, 1:34:27 AM11/20/15
to django...@googlegroups.com
Hi Monte,

You can use a model from one app in another app. 

So, let's say that in competitors/models.py (where you define the models for your "competitors" app), you have a model class named Participant. This model represents, well, a participant in the tournament. It is likely, as you stated, that you would need the Participant model (from the "competitors" app) in your "scoreboard" app when calculating scores for various participants. 

In this case, all you have to do is import the Participant model from competitors/models.py into your scoreboard/models.py file. This way, you can create relationships between the different models of the various apps of your tournaments project.

So, assuming that you have a Score model in your scoreboard/models.py (which you use to tie scores to a particular Participant), you can approach it like this:

> Make sure you have defined the Participant model in your competitors/models.py file.

For example, in your competitors/models.py, you may have:

class Participant(models.Model):
    name = models.CharField(max_length=90)
    team_name = models.CharField(max_length=75)
    signup_date = models.DateTimeField(auto_now_add=True)
    # Add other fields here

> In your scoreboard/models.py file, you may have something like:

from django.db import models
from competitors.models import Participant
#Add other imports from other files and apps' models that you need here


class Score(models.Model):
    participant = models.ForeignKey(Participant)
    field_x = models.CharField(max_length=123)
    another_field = models.PositiveIntegerField()


This way, when you query for scores through the Django ORM, the ORM also ties in the Participant from this queried score. You can then ask to get more info about the Participant from the participants table in the database. 

So, when you execute a query through the ORM, for example:
participant_a_scores = Score.objects.filter(participant__name="Monte")

To get the name of this team name of the participant whose scores you just queried for, you do:
monte_team = participant_a_scores.participant.team_name 

This query hits the "scores" table as well as the "participants" table to retrieve the score and the related Participant's info.

You may want to take a look at how Django handles relationships between models. Please, note that there is no difference in whether the related models are in the same app or from different apps. For details, please, take a look at documentation on: 



If I made it more confusing, please, let me know.

The best way to clear the fog on this is to go through the official tutorial, the model relationship docs (in the above link) and to implement these in your own project.

All the best. :)

Sincerely,
Muhammad 
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

memilanuk

unread,
Nov 20, 2015, 11:47:17 AM11/20/15
to django...@googlegroups.com
Outstanding, that made things a lot clearer! Now I just have to work
thru it in actual practice. Always seems to be the tricky part ;)

Thanks,

Monte

Reply all
Reply to author
Forward
0 new messages