return multiple fields for foreignkey

31 views
Skip to first unread message

Chantal Rosmuller

unread,
Nov 21, 2013, 4:41:57 AM11/21/13
to django...@googlegroups.com
Hi,

I'm pretty new to Django soi excuse me if this is a stupid question, but here we go.

I have the following model:

class Motivation(models.Model):
    def __unicode__(self):  # Python 3: def __str__(self):
        return self.score

    score           = models.CharField(max_length=100)
    description     = models.CharField(max_length=300)
    insert_date     = models.DateTimeField(auto_now_add=True) #insert date
    update_date     = models.DateTimeField(auto_now=True)  #last updated date

and another model with a foreign key to the first one:

class PerformanceAnimal(models.Model):
    def __unicode__(self):  # Python 3: def __str__(self):
         return u'%s' % self.animal

    training_session       = models.ForeignKey('Training')
    animal                 = models.ForeignKey('animals.Animal')
    state_of_mind          = models.CharField(max_length=200, blank=True)
    motivation             = models.ForeignKey('behaviour.Motivation')

In the admin panel the foreign key select field shows the score (like it's supposed to), but I would like it to show score, description

How do I go about that?

Thanks for your help.

Rafael E. Ferrero

unread,
Nov 21, 2013, 8:29:26 AM11/21/13
to django...@googlegroups.com
class Motivation(models.Model):
    def __unicode__(self):  # Python 3: def __str__(self):
        return '%s - %s' % (self.score, self.description)

    score           = models.CharField(max_length=100)
    description     = models.CharField(max_length=300)
    insert_date     = models.DateTimeField(auto_now_add=True) #insert date
    update_date     = models.DateTimeField(auto_now=True)  #last updated date


2013/11/21 Chantal Rosmuller <cros...@gmail.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...@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/db2ae4c7-4842-4e9d-afdc-376e2a0ad51b%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Rafael E. Ferrero
Reply all
Reply to author
Forward
0 new messages