本文整理汇总了Python中models.Article.headline方法的典型用法代码示例。如果您正苦于以下问题:Python Article.headline方法的具体用法?Python Article.headline怎么用?Python Article.headline使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Article
的用法示例。
在下文中一共展示了Article.headline方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_article_defaults
# 需要导入模块: from models import Article [as 别名]
# 或者: from models.Article import headline [as 别名]
def test_article_defaults(self):
# No articles are in the system yet.
self.assertEqual(len(Article.objects.all()), 0)
# Create an Article.
a = Article(id=None)
# Grab the current datetime it should be very close to the
# default that just got saved as a.pub_date
now = datetime.now()
# Save it into the database. You have to call save() explicitly.
a.save()
# Now it has an ID. Note it's a long integer, as designated by
# the trailing "L".
self.assertEqual(a.id, 1L)
# Access database columns via Python attributes.
self.assertEqual(a.headline, u'Default headline')
# make sure the two dates are sufficiently close
self.assertAlmostEqual(now, a.pub_date, delta=timedelta(5))
# make sure that SafeString/SafeUnicode fields work
a.headline = SafeUnicode(u'Iñtërnâtiônàlizætiøn1')
a.save()
a.headline = SafeString(u'Iñtërnâtiônàlizætiøn1'.encode('utf-8'))
a.save()
示例2: test_lookup
# 需要导入模块: from models import Article [as 别名]
# 或者: from models.Article import headline [as 别名]
def test_lookup(self):
# No articles are in the system yet.
self.assertQuerysetEqual(Article.objects.all(), [])
# Create an Article.
a = Article(
id=None,
headline='Area man programs in Python',
pub_date=datetime(2005, 7, 28),
)
# Save it into the database. You have to call save() explicitly.
a.save()
# Now it has an ID.
self.assertTrue(a.id != None)
# Models have a pk property that is an alias for the primary key
# attribute (by default, the 'id' attribute).
self.assertEqual(a.pk, a.id)
# Access database columns via Python attributes.
self.assertEqual(a.headline, 'Area man programs in Python')
self.assertEqual(a.pub_date, datetime(2005, 7, 28, 0, 0))
# Change values by changing the attributes, then calling save().
a.headline = 'Area woman programs in Python'
a.save()
# Article.objects.all() returns all the articles in the database.
self.assertQuerysetEqual(Article.objects.all(),
['<Article: Area woman programs in Python>'])
# Django provides a rich database lookup API.
self.assertEqual(Article.objects.get(id__exact=a.id), a)
self.assertEqual(Article.objects.get(headline__startswith='Area woman'), a)
self.assertEqual(Article.objects.get(pub_date__year=2005), a)
self.assertEqual(Article.objects.get(pub_date__year=2005, pub_date__month=7), a)
self.assertEqual(Article.objects.get(pub_date__year=2005, pub_date__month=7, pub_date__day=28), a)
self.assertEqual(Article.objects.get(pub_date__week_day=5), a)
# The "__exact" lookup type can be omitted, as a shortcut.
self.assertEqual(Article.objects.get(id=a.id), a)
self.assertEqual(Article.objects.get(headline='Area woman programs in Python'), a)
self.assertQuerysetEqual(
Article.objects.filter(pub_date__year=2005),
['<Article: Area woman programs in Python>'],
)
self.assertQuerysetEqual(
Article.objects.filter(pub_date__year=2004),
[],
)
self.assertQuerysetEqual(
Article.objects.filter(pub_date__year=2005, pub_date__month=7),
['<Article: Area woman programs in Python>'],
)
self.assertQuerysetEqual(
Article.objects.filter(pub_date__week_day=5),
['<Article: Area woman programs in Python>'],
)
self.assertQuerysetEqual(
Article.objects.filter(pub_date__week_day=6),
[],
)
# Django raises an Article.DoesNotExist exception for get() if the
# parameters don't match any object.
self.assertRaisesRegexp(
ObjectDoesNotExist,
"Article matching query does not exist.",
Article.objects.get,
id__exact=2000,
)
self.assertRaisesRegexp(
ObjectDoesNotExist,
"Article matching query does not exist.",
Article.objects.get,
pub_date__year=2005,
pub_date__month=8,
)
self.assertRaisesRegexp(
ObjectDoesNotExist,
"Article matching query does not exist.",
Article.objects.get,
pub_date__week_day=6,
)
# Lookup by a primary key is the most common case, so Django
# provides a shortcut for primary-key exact lookups.
# The following is identical to articles.get(id=a.id).
self.assertEqual(Article.objects.get(pk=a.id), a)
# pk can be used as a shortcut for the primary key name in any query.
self.assertQuerysetEqual(Article.objects.filter(pk__in=[a.id]),
["<Article: Area woman programs in Python>"])
#.........这里部分代码省略.........
示例3: test_object_creation
# 需要导入模块: from models import Article [as 别名]
# 或者: from models.Article import headline [as 别名]
def test_object_creation(self):
# Create an Article.
a = Article(
id=None,
headline='Area man programs in Python',
pub_date=datetime(2005, 7, 28),
)
# Save it into the database. You have to call save() explicitly.
a.save()
# You can initialize a model instance using positional arguments,
# which should match the field order as defined in the model.
a2 = Article(None, 'Second article', datetime(2005, 7, 29))
a2.save()
self.assertNotEqual(a2.id, a.id)
self.assertEqual(a2.headline, 'Second article')
self.assertEqual(a2.pub_date, datetime(2005, 7, 29, 0, 0))
# ...or, you can use keyword arguments.
a3 = Article(
id=None,
headline='Third article',
pub_date=datetime(2005, 7, 30),
)
a3.save()
self.assertNotEqual(a3.id, a.id)
self.assertNotEqual(a3.id, a2.id)
self.assertEqual(a3.headline, 'Third article')
self.assertEqual(a3.pub_date, datetime(2005, 7, 30, 0, 0))
# You can also mix and match position and keyword arguments, but
# be sure not to duplicate field information.
a4 = Article(None, 'Fourth article', pub_date=datetime(2005, 7, 31))
a4.save()
self.assertEqual(a4.headline, 'Fourth article')
# Don't use invalid keyword arguments.
self.assertRaisesRegexp(
TypeError,
"'foo' is an invalid keyword argument for this function",
Article,
id=None,
headline='Invalid',
pub_date=datetime(2005, 7, 31),
foo='bar',
)
# You can leave off the value for an AutoField when creating an
# object, because it'll get filled in automatically when you save().
a5 = Article(headline='Article 6', pub_date=datetime(2005, 7, 31))
a5.save()
self.assertEqual(a5.headline, 'Article 6')
# If you leave off a field with "default" set, Django will use
# the default.
a6 = Article(pub_date=datetime(2005, 7, 31))
a6.save()
self.assertEqual(a6.headline, u'Default headline')
# For DateTimeFields, Django saves as much precision (in seconds)
# as you give it.
a7 = Article(
headline='Article 7',
pub_date=datetime(2005, 7, 31, 12, 30),
)
a7.save()
self.assertEqual(Article.objects.get(id__exact=a7.id).pub_date,
datetime(2005, 7, 31, 12, 30))
a8 = Article(
headline='Article 8',
pub_date=datetime(2005, 7, 31, 12, 30, 45),
)
a8.save()
self.assertEqual(Article.objects.get(id__exact=a8.id).pub_date,
datetime(2005, 7, 31, 12, 30, 45))
# Saving an object again doesn't create a new object -- it just saves
# the old one.
current_id = a8.id
a8.save()
self.assertEqual(a8.id, current_id)
a8.headline = 'Updated article 8'
a8.save()
self.assertEqual(a8.id, current_id)
# Check that != and == operators behave as expecte on instances
self.assertTrue(a7 != a8)
self.assertFalse(a7 == a8)
self.assertEqual(a8, Article.objects.get(id__exact=a8.id))
self.assertTrue(Article.objects.get(id__exact=a8.id) != Article.objects.get(id__exact=a7.id))
self.assertFalse(Article.objects.get(id__exact=a8.id) == Article.objects.get(id__exact=a7.id))
# You can use 'in' to test for membership...
self.assertTrue(a8 in Article.objects.all())
#.........这里部分代码省略.........