Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@lazerscience is absolutely correct. You should tweak your models, since you are setting up a de facto many-to-many relationship; doing so will allow you to leverage more features of the admin interface &amp; so forth.</p> <p>Additionally, regardless, there is no need to go to <code>raw()</code>, since this can be done entirely via normal usage of the Django ORM.</p> <p>Something like:</p> <pre><code>class LadderPlayer(models.Model): player = models.ForeignKey(User, unique=True) position = models.IntegerField(unique=True) challenges = models.ManyToManyField("self", symmetrical=False, through='Match') class Match(models.Model): date = models.DateTimeField() challenger = models.ForeignKey(LadderPlayer) challengee = models.ForeignKey(LadderPlayer) </code></pre> <p>should be all you need to change in the models. You then should be able to do a query like</p> <pre><code>player_of_interest = LadderPlayer.objects.filter(pk=some_id) matches_of_interest = \ Match.objects.filter(Q(challenger__pk=some_id)|Q(challengee__pk=some_id)) </code></pre> <p>to get all the information of interest about the player in question. Note that you'll need to have <code>from django.db.models import Q</code> to use that.</p> <p>If you want <em>exactly</em> the same info you're presenting with your example query, I believe it'd be easiest to split the queries into separate ones for getting the challenger &amp; challengee lists -- for example, something like:</p> <pre><code>challengers = LadderPlayer.objects.filter(challenges__challengee__pk=poi_id) challenged_by = LadderPlayer.objects.filter(challenges__challenger__pk=poi_id) </code></pre> <p>will get the two relevant query sets for the player of interest (w/ a primary key of <code>poi_id</code>).</p> <p>If there's some particular reason you don't want the de facto many-to-many relationship to become a de jure one, you can change those to something along the lines of</p> <pre><code>challenger = LadderPlayer.objects.filter(match__challengee__pk=poi_id) challenged_by = LadderPlayer.objects.filter(match__challenger_pk=poi_id) </code></pre> <p>So the suggestion for the model change is merely to help leverage existing tools, and to make explicit a relationship which you are currently having occur implicitly.</p> <p>Based on how you want use it, you might want to do something like</p> <pre><code>pl_tuple = () for p in LadderPlayer.objects.all(): challengers = LadderPlayer.objects.filter(challenges__challengee__pk=p.id) challenged_by = LadderPlayer.objects.filter(challenges__challenger__pk=p.id) pl_tuple += (p.id, p.position, challengers, challenged_by) context_dict['ladder_players'] = pl_tuple </code></pre> <p>in your view to prepare the data for your template.</p> <p>Regardless, you should probably be doing your query through the Django ORM instead of using <code>raw()</code> in this case.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload