heya, Daniel: Thanks for the quick reply.
Hmm, ok, I made the correction to the code you recommended (changing str() to string interpolation). Do you know any good references on this, so I can get a better understanding of how these things are meant to work? For accessing the field, I found out my error. I was a bit silly, I think I tried to access the "description" field of both. However, one of those is a CharField, hence the fail...lol. Finally, for Trip, yeah, Trip is abstract, hence there aren't really any actual instances of it, I suppose. For the case here, do you think there's any advantages to moving from Abstract to Multi-Table Inheritance? Essentially, we have students, who each have addresses, and who have ToTrips and ReturnTrips between each other (and those addresses). The main operations we'll be doing is searching for and editing trips. Cheers, Victor On Oct 28, 3:11 am, Daniel Roseman <[email protected]> wrote: > On Oct 27, 2:49 pm, Victor Hooi <[email protected]> wrote: > > > > > 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 > > OK, firstly, you are returning bytestrings from the unicode methods, > instead of (obviously) unicode strings. This will eventually bite you: > if there's ever a value that contains a non-ASCII character (eg a > foreign address), it will die horribly. You should not be using str() > in your unicode methods. > > So, for example, in ToTrip, the method should look like this: > > def __unicode__(self): > return u'%s to %s' % (self.origin, self.destination) > > Now, in terms of your actual question, there's certainly nothing wrong > with doing 'return self.origin.description' - that certainly is the > way to access related fields. What happens when you try? Can you post > the exact traceback? > > Of course this will fail when the object does not have the foreign key > field set - eg when it's newly created. Perhaps this is the problem > you're experiencing. You should catch that possibility and return a > sensible default. > > On your second question, this depends on whether Trip is an abstract > class or not. If so, there are indeed no Trip objects to count. If > not, ie you are using multi-table inheritance, you can use > Trip.objects.count() to get the total number. > -- > DR. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

