Best way to get ForeignKey Related Objects?

19 views
Skip to first unread message

Tobias Dacoir

unread,
Jan 29, 2015, 8:51:16 AM1/29/15
to django...@googlegroups.com
I have two Models as shown below. Now when I have a specific AudioQuestionPair and I do something like

print pair.answers



it works just fine. However I can not do:

for answer in pair.answers



Using pair.answers.get.all() does also not work. So I have to do

answers
= Answer.objects.filter(audioQuestionPair=pair)



Isn't there a better way to design this? I can imagine that using objects.filter is more taxing on the database and uses more time than maybe another method?


class AudioQuestionPair(models.Model):
    audioData
= models.ForeignKey(AudioData)
    question
= models.ForeignKey(Question)
    database
= models.ForeignKey(Database)
    votes
= models.PositiveIntegerField(default=0)

   
class Meta:
        verbose_name
= "AudioData-Question Pair"
        verbose_name_plural
= "AudioData-Question Pairs"

   
def answers(self):
       
return Answer.objects.filter(audioQuestionPair=self.pk)

   
def __str__(self):
       
return "Database %s: AudioData %s - Question %s with %s Votes and Answers: %s" % (self.database, self.audioData, self.question, self.votes, self.answers())

"""
 Answer for a specific Audio-Question Pair
 cumulative
"""

class Answer(models.Model):
    body
= models.CharField(max_length=255)
    count
= models.PositiveIntegerField(default=0)
    isGroundtruth
= models.BooleanField(default=False)
    audioQuestionPair
= models.ForeignKey(AudioQuestionPair)

   
class Meta:
        verbose_name
= "Answer to AudioQuestionPair"
        verbose_name_plural
= "Answers to AudioQuestionPairs"

   
def __str__(self):
       
return "%s - %s times" % (self.body, self.count)



Vijay Khemlani

unread,
Jan 29, 2015, 8:59:56 AM1/29/15
to django...@googlegroups.com
"answers" seems to be a method on the AudioQuestionPair class, so your call should be:

for answer in pair.answers():
    print answer

and "pair.answers.get.all()" does not make sense sinse "answers" is a method.

If you don't want to use a specific method, you can do this:

answers = pair.answer_set.all()


--
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/e8991d3a-1d32-4655-918f-26e0877898de%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tobias Dacoir

unread,
Jan 29, 2015, 9:20:44 AM1/29/15
to django...@googlegroups.com
Damn, you are right. pair.answers() works. I'm wondering why I didn't get a syntax error when calling it without the parenthesis (), because print still worked.

Vijay Khemlani

unread,
Jan 29, 2015, 9:41:41 AM1/29/15
to django...@googlegroups.com
Python has higher order functions, so you can do things like this

m = pair.answers           # m refers to the method itself

print m        # Prints "method AudioQuestionPair.anwers of ..."

answers = m()              # Actually executes the method

Reply all
Reply to author
Forward
0 new messages