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
On Thu, Jan 29, 2015 at 11:20 AM, Tobias Dacoir <falc410@gmail.com> wrote:
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.--
On Thursday, January 29, 2015 at 2:59:56 PM UTC+1, Vijay Khemlani wrote:"answers" seems to be a method on the AudioQuestionPair class, so your call should be:for answer in pair.answers():print answerand "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()On Thu, Jan 29, 2015 at 10:51 AM, Tobias Dacoir <fal...@gmail.com> wrote: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)To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.--
You received this message because you are subscribed to the Google Groups "Django users" group.
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.
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/b66b75b7-4d6d-43ef-a72f-215b85e8c08c%40googlegroups.com.
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/CALn3ei1bPzZSah4LWw71gUmRg0fp-fxED-KhhRKMM0H3ABwcBg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment