Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Commit

Permalink
Use the gcharts manager as the default manager
Browse files Browse the repository at this point in the history
  • Loading branch information
rhblind committed Jul 24, 2014
1 parent 4a1b63f commit 654deae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
13 changes: 4 additions & 9 deletions README.md
Expand Up @@ -103,12 +103,8 @@ Register the GChartsManager to the model you'd like to draw charts from

class MyModel(models.Model):
# when using multiple managers, we need to specify the default 'objects' manager as well
# NOTE: Make sure to specify the default manager first, else wierd stuff can happen!
# See #Issue3
objects = models.Manager()
# register the GChartsManager as a manager for this model
gcharts = GChartsManager()
# register the GChartsManager as default manager for this model.
objects = GChartsManager()
my_field = models.CharField(....)
my_other_field = models.IntegerField()
Expand All @@ -127,8 +123,7 @@ Spam Inc. needs to chart how much spam they sell.

class Spam(models.Model):

objects = models.Manager()
gcharts = GChartsManager()
objects = GChartsManager()

name = models.Charfield(max_length=10)
...
Expand Down Expand Up @@ -156,7 +151,7 @@ Spam Inc. needs to chart how much spam they sell.
# - values() to extract fields of interest
# - annotate() to group aggregate Count into 'id__count'
# - order_by() to make the aggregate work
qset = Spam.gcharts.filter(cdt__gt=series_age).extra(select={"date": "cdt::date"}) \
qset = Spam.objects.filter(cdt__gt=series_age).extra(select={"date": "cdt::date"}) \
.values("date").annotate(Count("id")).order_by()

# Call the qset.to_json() method to output the data in json
Expand Down
15 changes: 6 additions & 9 deletions demosite/models.py
Expand Up @@ -6,10 +6,9 @@ class GeoData(models.Model):
"""
A model which contains data for the GeoChart
"""

gcharts = GChartsManager()
objects = models.Manager()


objects = GChartsManager()

country_name = models.CharField(max_length=100)
country_code = models.CharField(max_length=2)
population = models.PositiveIntegerField()
Expand All @@ -20,12 +19,10 @@ class OtherData(models.Model):
"""
A model which contains some other random data
"""

gcharts = GChartsManager()
objects = models.Manager()


objects = GChartsManager()

name = models.CharField(max_length=20)
number1 = models.IntegerField()
number2 = models.IntegerField()
date = models.DateField()

8 changes: 4 additions & 4 deletions demosite/views.py
Expand Up @@ -14,7 +14,7 @@ def home(request):
if request.method == "GET":

# GeoChart demo
geo_qset = GeoData.gcharts.order_by("-population").all()[:100]
geo_qset = GeoData.objects.order_by("-population").all()[:100]
geo_data = geo_qset.values("country_name", "population", "fertility_rate") \
.to_json(labels={"country_name": "Country",
"population": "Population",
Expand All @@ -25,7 +25,7 @@ def home(request):

# AreaChart
area_series_age = datetime.today() - relativedelta(months=1)
area_qset = OtherData.gcharts.filter(date__gte=area_series_age) \
area_qset = OtherData.objects.filter(date__gte=area_series_age) \
.values("date").annotate(Sum("number1")) \
.annotate(Sum("number2")).order_by()
area_data = area_qset.order_by("-date").to_json(order=("date", "number1__sum",
Expand All @@ -34,7 +34,7 @@ def home(request):
"number2__sum": "Another number"})

# PieChart
pie_qset = OtherData.gcharts.values("name").annotate(Sum("number1")).order_by()
pie_qset = OtherData.objects.values("name").annotate(Sum("number1")).order_by()
pie_data = pie_qset.order_by("name").to_json(order=("name", "number1__sum"),
formatting={"number1__sum": "{0:d} Sum total"})

Expand All @@ -43,7 +43,7 @@ def home(request):

# Table
table_series_age = datetime.today() - relativedelta(months=3)
table_qset = OtherData.gcharts.filter(date__gte=table_series_age) \
table_qset = OtherData.objects.filter(date__gte=table_series_age) \
.values("date").annotate(num_baked=Sum("number1")) \
.annotate(num_eaten=Sum("number2")).order_by("-date")
table_data = table_qset.to_json(labels={"date": "Date",
Expand Down

0 comments on commit 654deae

Please sign in to comment.