From da5b0586d359390e2102703707afae65befe7f6d Mon Sep 17 00:00:00 2001 From: Jason Pellerin Date: Wed, 28 Jun 2006 17:07:26 +0000 Subject: Merge trunk to [3226] git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3228 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/basic/models.py | 5 ++ tests/modeltests/lookup/models.py | 4 ++ tests/modeltests/serializers/__init__.py | 0 tests/modeltests/serializers/models.py | 94 ++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 tests/modeltests/serializers/__init__.py create mode 100644 tests/modeltests/serializers/models.py (limited to 'tests') diff --git a/tests/modeltests/basic/models.py b/tests/modeltests/basic/models.py index a4de0f9a81..78d943eb97 100644 --- a/tests/modeltests/basic/models.py +++ b/tests/modeltests/basic/models.py @@ -347,4 +347,9 @@ API_TESTS += """ >>> a101 = Article.objects.get(pk=101) >>> a101.headline 'Article 101' + +# You can create saved objects in a single step +>>> a10 = Article.objects.create(headline="Article 10", pub_date=datetime(2005, 7, 31, 12, 30, 45)) +>>> Article.objects.get(headline="Article 10") + """ diff --git a/tests/modeltests/lookup/models.py b/tests/modeltests/lookup/models.py index e0c4850ba2..a2c0a14158 100644 --- a/tests/modeltests/lookup/models.py +++ b/tests/modeltests/lookup/models.py @@ -58,6 +58,10 @@ Article 4 >>> Article.objects.filter(headline__startswith='Blah blah').count() 0L +# Date and date/time lookups can also be done with strings. +>>> Article.objects.filter(pub_date__exact='2005-07-27 00:00:00').count() +3L + # in_bulk() takes a list of IDs and returns a dictionary mapping IDs # to objects. >>> Article.objects.in_bulk([1, 2]) diff --git a/tests/modeltests/serializers/__init__.py b/tests/modeltests/serializers/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/modeltests/serializers/models.py b/tests/modeltests/serializers/models.py new file mode 100644 index 0000000000..8c9483beba --- /dev/null +++ b/tests/modeltests/serializers/models.py @@ -0,0 +1,94 @@ +""" +XXX. Serialization + +``django.core.serializers`` provides interfaces to converting Django querysets +to and from "flat" data (i.e. strings). +""" + +from django.db import models + +class Category(models.Model): + name = models.CharField(maxlength=20) + + class Meta: + ordering = ('name',) + + def __str__(self): + return self.name + +class Author(models.Model): + name = models.CharField(maxlength=20) + + class Meta: + ordering = ('name',) + + def __str__(self): + return self.name + +class Article(models.Model): + author = models.ForeignKey(Author) + headline = models.CharField(maxlength=50) + pub_date = models.DateTimeField() + categories = models.ManyToManyField(Category) + + class Meta: + ordering = ('pub_date',) + + def __str__(self): + return self.headline + +API_TESTS = """ +# Create some data: +>>> from datetime import datetime +>>> sports = Category(name="Sports") +>>> music = Category(name="Music") +>>> op_ed = Category(name="Op-Ed") +>>> sports.save(); music.save(); op_ed.save() + +>>> joe = Author(name="Joe") +>>> jane = Author(name="Jane") +>>> joe.save(); jane.save() + +>>> a1 = Article( +... author = jane, +... headline = "Poker has no place on ESPN", +... pub_date = datetime(2006, 6, 16, 11, 00)) +>>> a2 = Article( +... author = joe, +... headline = "Time to reform copyright", +... pub_date = datetime(2006, 6, 16, 13, 00)) +>>> a1.save(); a2.save() +>>> a1.categories = [sports, op_ed] +>>> a2.categories = [music, op_ed] + +# Serialize a queryset to XML +>>> from django.core import serializers +>>> xml = serializers.serialize("xml", Article.objects.all()) + +# The output is valid XML +>>> from xml.dom import minidom +>>> dom = minidom.parseString(xml) + +# Deserializing has a similar interface, except that special DeserializedObject +# instances are returned. This is because data might have changed in the +# database since the data was serialized (we'll simulate that below). +>>> for obj in serializers.deserialize("xml", xml): +... print obj + + + +# Deserializing data with different field values doesn't change anything in the +# database until we call save(): +>>> xml = xml.replace("Poker has no place on ESPN", "Poker has no place on television") +>>> objs = list(serializers.deserialize("xml", xml)) + +# Even those I deserialized, the database hasn't been touched +>>> Article.objects.all() +[, ] + +# But when I save, the data changes as you might except. +>>> objs[0].save() +>>> Article.objects.all() +[, ] + +""" -- cgit v1.3