Thursday, November 19, 2015

Re: Questions on project vs app, and how they relate

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 

On Fri, Nov 20, 2015 at 12:30 AM, memilanuk <memilanuk@gmail.com> wrote:
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?

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/n2mb5e%244u2%241%40ger.gmane.org.

For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAJOFuZyPxw6DT5kawn0XKwjNE2d_S1sWQt%3DFv5FURNxauvUg_A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment