Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting python methods that call class methods
    primarykey
    data
    text
    <p>I have a very simple method:</p> <pre><code>Class Team(models.Model): def sides(self): return SideNames.objects.filter(team=self) </code></pre> <p>SideNames is another model defined in the same file as Team, </p> <p>Which when I try and test:</p> <pre><code>self.assertEquals(len(t.sides()), 2) </code></pre> <p>I get the following error:</p> <blockquote> <pre><code>return SideNames.objects.filter(team=self) </code></pre> <p>AttributeError: 'NoneType' object has no attribute 'objects'</p> </blockquote> <p>but if I change the test to be</p> <pre><code>self.assertEquals(len(SideNames.objects.filter(team=t)), 2) </code></pre> <p>Then I don't get the error. What's the difference between calling SideNames.objects.filter from the test itself and calling the actual method?</p> <p>For reference, here are the 2 classes in their entirety.</p> <pre><code>class Team(models.Model): """The model for a football team.""" class Admin: pass def __unicode__(self): return u'%s' % self.name def is_player(self, player): """Checks to see if 'player' is a member if this team. Returns True if they are, or False otherwise.""" try: teamPlayer = TeamPlayers.objects.get(player=player, team=self) return True except ObjectDoesNotExist: return False def sides(self): """Return the side names for this team""" return SideNames.objects.filter(team=self) def updateSides(self, side_a, side_b): """Update the side names""" names = SideNames.objects.filter(team=self); a = SideNames.objects.get(name = names[0].name) a.name = side_a a.save() b = SideNames.objects.get(name = names[1].name) b.name = side_b b.save() name = models.CharField("Team Name", max_length=255) organiser = models.ForeignKey(User) class SideNames(models.Model): """Holds the names of the sides for each team""" class Admin: pass def __unicode__(self): """Pretty print the SideNames object""" return self.name team = models.ForeignKey(Team) name = models.CharField(max_length=128) </code></pre>
    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.
 

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