Yes, due to the many-to-many, the "member" property is going to
represented as a set of objects. Your __str__ definition as you have
it doesn't really make sense since it is in fact a set of objects.
Try something like:
def __str__(self):
return '%s: ' % self.title + ','.join( [ '%s %s' %
(m.first_name,m.last_name) for m in self.member.objects.all() ] )
Your thinking is correct - if it was a ForeignKey then you could
simply reference the Person properties via self.member.<property>
-Brian
On Apr 20, 10:12 am, Jason Murray <[EMAIL PROTECTED]> wrote:
> This may seem like a pretty basic question, but I can't seem to figure
> it out.
>
> Is it possible to have a class's __str__ function access variables from
> other classes in a model?
>
> For example:
>
> class Person(models.Model):
> first_name = models.CharField(maxlength=30)
> last_name = models.CharField(maxlength=30)
> def __str__(self):
> return self.first_name + " " + self.last_name
> class Admin:
> pass
>
> class Executive(models.Model):
> title = models.CharField(maxlength=30)
> member = models.ManyToManyField(Person)
> def __str__(self):
> return self.title + member.first_name + member.last_name
> class Admin:
> pass
>
> This does not work. I've already tried. Sorry I don't have th error
> message anymore. I could recreate it if needed.
>
> Does it have to do with the ManyToMany? What if it was a ForeignKey
> instead?
>
> Thanks in advance.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---