heya,

In my models.py, I've got an "Address", as well as an abstract "Trip"
object, and then a "ToTrip", and "ReturnTrip" object that derive from
that. ToTrip and ReturnTrip both contain references to two Address
objects (models.ForeignKey).

class Address(models.Model):
    description = models.CharField(max_length=20, help_text='Helpful
description of this location. Maximum 20 characters.')
    ...(other fields and methods omitted)

class ToTrip(Trip):
    origin = models.ForeignKey(Address)
    destination = models.CharField(max_length=20, choices=CAMPUSES)

    # Todo: Is there a cleaner way of doing this?
    def __unicode__(self):
        return str(self.origin) + ' to ' + str(self.destination)


class ReturnTrip(Trip):
    origin = models.CharField(max_length=20, choices=CAMPUSES)
    destination = models.ForeignKey(Address)

    # Todo: Is there a cleaner way of doing this?
    def __unicode__(self):
        return str(self.origin) + ' to ' + str(self.destination)

In Address, there's a __unicode__() that returns a nicely formatted
string.

My first question is, for ToTrip and ReturnTrip, I wanted to create a
a __unicode__ method that just returned a description field inside of
address. My attempts at using:

    return self.origin.description

failed, and I'm guessing that's not the right way to access a field
through a FK relationship? In the above, I'm just using the
__unicode__ method on each address to get it - but I'm sure there's a
cleaner way to get access to the related object's fields themselves?

My second question is a little trivial - is there a way to count the
total number of Trip objects, or objects that are inheritting from it?
At the moment, I'm just counting ReturnTrip and ToTrip objects, and
adding them. No biggie, but I was wondering if there was a way to get
the total number of Trip objects, or at least the number of children?
(I'm guessing the number of actual Trip objects themselves would be
zero?)

Cheers,
Victor
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to