tags:

views:

31

answers:

1

Given the following model:

class Project(models.Model):
    project_name = models.CharField(max_length=255)
    abstract = models.TextField(blank=True, null=True)
    full_description = models.TextField(blank=True, null=True)
    date_begun = models.DateField(blank=True, null=True)
    related_projects = models.ManyToManyField('self', blank=True, null=True)
    class Meta:
        ordering = ['project_name']
    def __unicode__(self):
        return self.project_name

How do I access the ID of projects references in the related_projects field. For example, I can get their project_name values by doing something like this:

def transform_related_projects(self, instance):
    return [unicode(rp) for rp in instance.related_projects.all()]

But I can't see how to get the if for the Project record, since the def unicode(self) function only return the project name as a unicode string. I know I'm missing something obvious. Thanks

A: 
def transform_related_projects(self, instance):
    return [rp.id for rp in instance.related_projects.all()]
Brian Luft
Sure enough, simple. I had tried return [unicode(rp) for rp in instance.related_projects.id.all()], and when it didn't work I thought I was totally off-track. Still learning the basic concepts of python I guess. Thanks!
andyashton