Search code examples
google-app-enginegoogle-cloud-datastoremode

Is there a better way to model this relationship?


I have these models:

class Assignment(db.Model):
    title = db.StringPeoperty(required=True)
    ...other stuff...

class TeamScore(db.Model):
    assignment = db.ReferenceProperty(Assignment, 
                                      collection_name="teamScores",
                                      required=True)
    teamID = db.IntegerProperty(required = True)
    score = db.IntegerProperty(required = True)
    ...other stuff...

Here is the peoblem. In the homepage, I want to show the top three( based on score) team work of each assignment. So, I pass this to my template:

assignments = Assignment.all()

In the template:

{% for assignment in assignments%}
    **<!--How to implement showing the top three-->**
    {% for score in assignment.teamScores%}
         {{score.teamID}}
         {{score.score}}
         ...............
    {% endfor%}
{%endfor%}

With this datastore model. I need to sort in template, it's impossible. So, I think it may have a better model to handle this problem. But I can't figure it out. I thought about this idea that add a ListProperty to store the top three. Everytime a new TeamScore generated, I will compare this to the scores in the List to keep the top three fresh. But I think this way is not elengance.

Can you give me some suggestion?

Thank you!


Solution

  • This isn't a data modelling issue, it's a templating one. You need to fetch the assignments and perform the queries to get the list of scores in your code, and pass the results into the template, rather than just passing in the assignment model and doing the rest of the work in your template.