App Engine Python SDK version 1.7.4 (2)
[gae.git] / python / lib / django_1_4 / tests / modeltests / custom_methods / models.py
blob4e3da588519a9e5ccc3c3e13d50a6fee934f3de0
1 """
2 3. Giving models custom methods
4 Any method you add to a model will be available to instances.
5 """
7 import datetime
9 from django.db import models
12 class Article(models.Model):
13 headline = models.CharField(max_length=100)
14 pub_date = models.DateField()
16 def __unicode__(self):
17 return self.headline
19 def was_published_today(self):
20 return self.pub_date == datetime.date.today()
22 def articles_from_same_day_1(self):
23 return Article.objects.filter(pub_date=self.pub_date).exclude(id=self.id)
25 def articles_from_same_day_2(self):
26 """
27 Verbose version of get_articles_from_same_day_1, which does a custom
28 database query for the sake of demonstration.
29 """
30 from django.db import connection
31 cursor = connection.cursor()
32 cursor.execute("""
33 SELECT id, headline, pub_date
34 FROM custom_methods_article
35 WHERE pub_date = %s
36 AND id != %s""", [connection.ops.value_to_db_date(self.pub_date),
37 self.id])
38 return [self.__class__(*row) for row in cursor.fetchall()]