I'm not sure about your models, but I assume it is something like the
following:

from django.db import models

class Holding(models.Model):
    symbol = models.CharField(maxlength=10)

    def __str__(self):
        return self.symbol

    def _get_latest_price(self):
        return self.price_set.all()[0]

    get_latest_price = property(_get_latest_price)

    class Admin:
        pass

class Price(models.Model):
    holding = models.ForeignKey(Holding)
    price = models.FloatField(max_digits=6,decimal_places=2)
    pdate = models.DateField()

    def __str__(self):
        return "%s - %s - %s" %
(self.holding.symbol,self.pdate,self.price)

    class Admin:
        pass

    class Meta:
        ordering = ("-pdate",)

For it, I've added ordering to the Meta for Price, so it always returns
the prices in descending order of date. At that point, the
event_set.all()[0] for a given Holding will give its latest price. I
added a _get_latest_price() convenience method (and made it a property)
just for giggles, but that should allow you to do use something like
myholding.get_latest_price.price (as well as accessing the date) for a
holding of your choice.

Will that do the trick?

By the way, it seems like having the symbol in your Price model would
be redundant, since you can use myprice.holding.symbol to get the
symbol if you are listing (for example) all of the prices for a given
day.

H.B.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to