OK thanks. I didn't realise that was the implication of the related_name field. All working as expected now.
Thanks, Ben On 28 Feb, 13:16, Daniel Roseman <dan...@roseman.org.uk> wrote: > On Feb 28, 11:46 am, Ben Dowling <ben.m.dowl...@googlemail.com> wrote: > > > > > I have a Thread and a Message model, as defined below: > > > class Thread(models.Model): > > modified = models.DateTimeField(auto_now=True, auto_now_add=True) > > > class Message(models.Model): > > subject = models.CharField(max_length = 100) > > thread = models.ForeignKey(Thread, related_name='thread', > > null=True) > > content = models.TextField() > > sent = models.DateTimeField('timestamp', auto_now_add=True) > > read = models.BooleanField(default = 0) > > sender = models.ForeignKey(User, related_name='sender') > > thread = models.ForeignKey(Thread, related_name='thread', null=True) > > recipient = models.ForeignKey(User, related_name='recipient') > > > def save(self, *args, **kwargs): > > if self.thread is None: > > self.thread = Thread() > > self.thread.save() > > super(Message, self).save(*args, **kwargs) > > > I'd expect to be able all the messages for a given thread with > > thread.message_set, but I get the following AttributeError when i try > > that: 'Thread' object has no attribute 'message_set'. Any suggestions > > on why? > > > Thanks, Ben > > You've explictly set a related_name on the thread foreignkey, which is > what is used to refer to the reverse relation. So it should be: > > thread.thread.all() > > which is pretty confusing - you're better off dropping the > related_name and going back to the default, message_set. > -- > DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.