On Sun, Jul 6, 2008 at 5:53 PM, Christopher Clarke <[EMAIL PROTECTED]> wrote:
> Hi Guys
> I'm building a django based system for a company that monitors the mutuals
> funds industry
> We have Mutual Funds Companies (Issuer) who have one or more funds (Fund)
> and every month the funds submit a volume report
> Here is a simplified version
> ========================
> class Issuer(models.Model):
> name=models.CharField(max_length=100,unique=True)
> --
> --
> --
> class Fund(models.Model):
> issuer = models.ForeignKey(Issuer)
> symbol = models.CharField(max_length=15,unique=True)
> --
> --
> class FundVolumeReport(models.Model):
>
> fund= models.ForeignKey(Fund)
> period_ending=models.DateField()
>
>
> units_purchased_individuals=models.FloatField(verbose_name="Individuals",blank=True,null=True)
>
>
> units_purchased_institutions=models.FloatField(verbose_name="Instutions",blank=True,null=True)
> --
> --
> Now we need to produce totals for each Issuer by month i implemented a
> date based generic view using a model (IssuerVolumeAggregates) which wrapped
> a database view which did the aggregation i.e
> CREATE OR REPLACE VIEW core_issuervolumeaggregates AS
> select
>
> trim(to_char(i.id,'999'))||trim(to_char(vd.period_ending_id::int,'9999'))::int
> as id,
> i.id as issuer_id,
> max(i.name)as name,
> m.dateix as period_ending,
> count(*) as no_funds
> --
> --
> from core_fundvolumedata vd,
> core_fund f, core_issuer i, freqdates_monthly m
> where f.id=vd.fund_id and f.issuer_id=i.id
> and m.id=vd.period_ending_id
> group by i.id,vd.period_ending_id,m.dateix
> order by i.id,m.dateix;
> This works pretty well but its kind a clunky so i was wondering if i could
> do the same thing with a custom manager
> ======================================================
> class IssuerManaager(models.Manager):
>
> def with_aggs(self):
> from django.db import connection
> cursor=connection.cursor()
> cursor.execute("""
> select i.id,max(i.name), m.dateix AS period_ending,
> count(*) AS no_funds,
> sum(vd.total_units_issued_outstanding) as
> total_units_issued_outstanding,
> sum(vd.total_unit_holders) as no_unit_holders,
> sum(vd.tt_total_net_assets_under_management) AS
> net_assets_under_management,
> sum(vd.tt_value_redemptions) AS total_redemptions,
> sum(vd.tt_value_sales) AS total_sales
> FROM core_fundvolumedata vd, core_fund f, core_issuer i,
> freqdates_monthly m
> WHERE f.id = vd.fund_id AND f.issuer_id = i.id AND m.id =
> vd.period_ending_id
> GROUP BY i.id, m.dateix, vd.period_ending_id
> ORDER BY i.id, m.dateix;
> """)
> results_list= []
> for each in cursor.fetchall():
> p= self.model(id=each[0],name=each[1])
> p.period_ending=each[2]
> p.no_funds = each[3]
> p.units_issued_outstanding=each[4]
> p.no_unit_holders=each[5]
> p.net_assets_under_management=each[6]
> p.redemptions=each[7]
> p.sales=each[8]
> results_list.append(p)
> return results_list
>
> class Issuer(models.Model):
> name = models.CharField(max_length=100)
> --
> --
> --
> objects = IssuerManaager()
> The manager works but when i try to use it in the generic view
> return archive_month(
> request,
> year=year,
> month=month,
> queryset = Issuer.objects.with_aggs(),
> date_field = 'period_ending',
> I get
> list' object has no attribute 'model'
> Am i on the right track and how do i get this to work??
> Thanks for any help
Hrm, can you include a complete traceback? Looks like you are on the
right track, just looking at
http://www.djangoproject.com/documentation/model-api/#custom-managers
.
--
Milan
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---