mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Merge branch 'master' into schema-alteration
This commit is contained in:
@@ -1,22 +1,26 @@
|
||||
# coding: utf-8
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Author(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
age = models.IntegerField()
|
||||
friends = models.ManyToManyField('self', blank=True)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Publisher(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
num_awards = models.IntegerField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Book(models.Model):
|
||||
isbn = models.CharField(max_length=9)
|
||||
name = models.CharField(max_length=255)
|
||||
@@ -28,15 +32,16 @@ class Book(models.Model):
|
||||
publisher = models.ForeignKey(Publisher)
|
||||
pubdate = models.DateField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Store(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
books = models.ManyToManyField(Book)
|
||||
original_opening = models.DateTimeField()
|
||||
friday_night_closing = models.TimeField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
@@ -5,8 +5,10 @@
|
||||
This is a basic model with only two non-primary-key fields.
|
||||
"""
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100, default='Default headline')
|
||||
pub_date = models.DateTimeField()
|
||||
@@ -14,5 +16,5 @@ class Article(models.Model):
|
||||
class Meta:
|
||||
ordering = ('pub_date','headline')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
@@ -10,6 +10,7 @@ field. This method returns the "human-readable" value of the field.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
GENDER_CHOICES = (
|
||||
@@ -17,9 +18,10 @@ GENDER_CHOICES = (
|
||||
('F', 'Female'),
|
||||
)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@@ -18,24 +18,27 @@ from the default generated name, use the ``db_table`` parameter on the
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Author(models.Model):
|
||||
first_name = models.CharField(max_length=30, db_column='firstname')
|
||||
last_name = models.CharField(max_length=30, db_column='last')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '%s %s' % (self.first_name, self.last_name)
|
||||
|
||||
class Meta:
|
||||
db_table = 'my_author_table'
|
||||
ordering = ('last_name','first_name')
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100)
|
||||
authors = models.ManyToManyField(Author, db_table='my_m2m_table')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
class Meta:
|
||||
|
||||
@@ -12,6 +12,7 @@ returns.
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
# An example of a custom manager called "objects".
|
||||
|
||||
@@ -19,13 +20,14 @@ class PersonManager(models.Manager):
|
||||
def get_fun_people(self):
|
||||
return self.filter(fun=True)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
first_name = models.CharField(max_length=30)
|
||||
last_name = models.CharField(max_length=30)
|
||||
fun = models.BooleanField()
|
||||
objects = PersonManager()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s %s" % (self.first_name, self.last_name)
|
||||
|
||||
# An example of a custom manager that sets get_query_set().
|
||||
@@ -34,6 +36,7 @@ class PublishedBookManager(models.Manager):
|
||||
def get_query_set(self):
|
||||
return super(PublishedBookManager, self).get_query_set().filter(is_published=True)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Book(models.Model):
|
||||
title = models.CharField(max_length=50)
|
||||
author = models.CharField(max_length=30)
|
||||
@@ -41,7 +44,7 @@ class Book(models.Model):
|
||||
published_objects = PublishedBookManager()
|
||||
authors = models.ManyToManyField(Person, related_name='books')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
# An example of providing multiple custom managers.
|
||||
@@ -50,6 +53,7 @@ class FastCarManager(models.Manager):
|
||||
def get_query_set(self):
|
||||
return super(FastCarManager, self).get_query_set().filter(top_speed__gt=150)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Car(models.Model):
|
||||
name = models.CharField(max_length=10)
|
||||
mileage = models.IntegerField()
|
||||
@@ -57,5 +61,5 @@ class Car(models.Model):
|
||||
cars = models.Manager()
|
||||
fast_cars = FastCarManager()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@@ -7,13 +7,15 @@ Any method you add to a model will be available to instances.
|
||||
import datetime
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100)
|
||||
pub_date = models.DateField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
def was_published_today(self):
|
||||
|
||||
@@ -3,8 +3,10 @@ import string
|
||||
|
||||
from django.db import models
|
||||
from django.utils import six
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class MyWrapper(object):
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
@@ -12,7 +14,7 @@ class MyWrapper(object):
|
||||
def __repr__(self):
|
||||
return "<%s: %s>" % (self.__class__.__name__, self.value)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.value
|
||||
|
||||
def __eq__(self, other):
|
||||
@@ -20,8 +22,7 @@ class MyWrapper(object):
|
||||
return self.value == other.value
|
||||
return self.value == other
|
||||
|
||||
class MyAutoField(models.CharField):
|
||||
__metaclass__ = models.SubfieldBase
|
||||
class MyAutoField(six.with_metaclass(models.SubfieldBase, models.CharField)):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['max_length'] = 10
|
||||
@@ -30,7 +31,7 @@ class MyAutoField(models.CharField):
|
||||
def pre_save(self, instance, add):
|
||||
value = getattr(instance, self.attname, None)
|
||||
if not value:
|
||||
value = MyWrapper(''.join(random.sample(string.lowercase, 10)))
|
||||
value = MyWrapper(''.join(random.sample(string.ascii_lowercase, 10)))
|
||||
setattr(instance, self.attname, value)
|
||||
return value
|
||||
|
||||
|
||||
@@ -11,8 +11,10 @@ from __future__ import absolute_import, unicode_literals
|
||||
from django.db import models
|
||||
|
||||
from .fields import MyAutoField
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Employee(models.Model):
|
||||
employee_code = models.IntegerField(primary_key=True, db_column = 'code')
|
||||
first_name = models.CharField(max_length=20)
|
||||
@@ -20,22 +22,24 @@ class Employee(models.Model):
|
||||
class Meta:
|
||||
ordering = ('last_name', 'first_name')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s %s" % (self.first_name, self.last_name)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Business(models.Model):
|
||||
name = models.CharField(max_length=20, primary_key=True)
|
||||
employees = models.ManyToManyField(Employee)
|
||||
class Meta:
|
||||
verbose_name_plural = 'businesses'
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Bar(models.Model):
|
||||
id = MyAutoField(primary_key=True, db_index=True)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return repr(self.pk)
|
||||
|
||||
|
||||
|
||||
@@ -3,18 +3,20 @@ Tests for defer() and only().
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
class Secondary(models.Model):
|
||||
first = models.CharField(max_length=50)
|
||||
second = models.CharField(max_length=50)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Primary(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
value = models.CharField(max_length=50)
|
||||
related = models.ForeignKey(Secondary)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Child(Primary):
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class R(models.Model):
|
||||
is_default = models.BooleanField(default=False)
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Tag(models.Model):
|
||||
name = models.CharField(max_length=10)
|
||||
parent = models.ForeignKey('self', blank=True, null=True,
|
||||
@@ -10,19 +12,21 @@ class Tag(models.Model):
|
||||
class Meta:
|
||||
ordering = ['name']
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Celebrity(models.Model):
|
||||
name = models.CharField("Name", max_length=20)
|
||||
greatest_fan = models.ForeignKey("Fan", null=True, unique=True)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Fan(models.Model):
|
||||
fan_of = models.ForeignKey(Celebrity)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Staff(models.Model):
|
||||
id = models.IntegerField(primary_key=True)
|
||||
name = models.CharField(max_length=50)
|
||||
@@ -30,12 +34,13 @@ class Staff(models.Model):
|
||||
tags = models.ManyToManyField(Tag, through='StaffTag')
|
||||
coworkers = models.ManyToManyField('self')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class StaffTag(models.Model):
|
||||
staff = models.ForeignKey(Staff)
|
||||
tag = models.ForeignKey(Tag)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s -> %s" % (self.tag, self.staff)
|
||||
|
||||
@@ -4,15 +4,18 @@ Tests for F() query expression syntax.
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Employee(models.Model):
|
||||
firstname = models.CharField(max_length=50)
|
||||
lastname = models.CharField(max_length=50)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '%s %s' % (self.firstname, self.lastname)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Company(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
num_employees = models.PositiveIntegerField()
|
||||
@@ -25,5 +28,5 @@ class Company(models.Model):
|
||||
related_name='company_point_of_contact_set',
|
||||
null=True)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@@ -13,11 +13,13 @@ field.
|
||||
from datetime import datetime
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100, default='Default headline')
|
||||
pub_date = models.DateTimeField(default=datetime.now)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
@@ -5,8 +5,10 @@ import json
|
||||
from django.db import models
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils import six
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Small(object):
|
||||
"""
|
||||
A simple class to show that non-trivial Python objects can be used as
|
||||
@@ -15,19 +17,15 @@ class Small(object):
|
||||
def __init__(self, first, second):
|
||||
self.first, self.second = first, second
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '%s%s' % (force_text(self.first), force_text(self.second))
|
||||
|
||||
def __str__(self):
|
||||
return six.text_type(self).encode('utf-8')
|
||||
|
||||
class SmallField(models.Field):
|
||||
class SmallField(six.with_metaclass(models.SubfieldBase, models.Field)):
|
||||
"""
|
||||
Turns the "Small" class into a Django field. Because of the similarities
|
||||
with normal character fields and the fact that Small.__unicode__ does
|
||||
something sensible, we don't need to implement a lot here.
|
||||
"""
|
||||
__metaclass__ = models.SubfieldBase
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['max_length'] = 2
|
||||
@@ -57,8 +55,7 @@ class SmallerField(SmallField):
|
||||
pass
|
||||
|
||||
|
||||
class JSONField(models.TextField):
|
||||
__metaclass__ = models.SubfieldBase
|
||||
class JSONField(six.with_metaclass(models.SubfieldBase, models.TextField)):
|
||||
|
||||
description = ("JSONField automatically serializes and desializes values to "
|
||||
"and from JSON.")
|
||||
|
||||
@@ -8,13 +8,15 @@ from django.db import models
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
from .fields import SmallField, SmallerField, JSONField
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class MyModel(models.Model):
|
||||
name = models.CharField(max_length=10)
|
||||
data = SmallField('small field')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return force_text(self.name)
|
||||
|
||||
class OtherModel(models.Model):
|
||||
|
||||
@@ -20,7 +20,7 @@ class FileStorageTests(TestCase):
|
||||
shutil.rmtree(temp_storage_location)
|
||||
|
||||
def test_files(self):
|
||||
temp_storage.save('tests/default.txt', ContentFile(b'default content'))
|
||||
temp_storage.save('tests/default.txt', ContentFile('default content'))
|
||||
# Attempting to access a FileField from the class raises a descriptive
|
||||
# error
|
||||
self.assertRaises(AttributeError, lambda: Storage.normal)
|
||||
@@ -31,7 +31,7 @@ class FileStorageTests(TestCase):
|
||||
self.assertRaises(ValueError, lambda: obj1.normal.size)
|
||||
|
||||
# Saving a file enables full functionality.
|
||||
obj1.normal.save("django_test.txt", ContentFile(b"content"))
|
||||
obj1.normal.save("django_test.txt", ContentFile("content"))
|
||||
self.assertEqual(obj1.normal.name, "tests/django_test.txt")
|
||||
self.assertEqual(obj1.normal.size, 7)
|
||||
self.assertEqual(obj1.normal.read(), b"content")
|
||||
@@ -59,7 +59,7 @@ class FileStorageTests(TestCase):
|
||||
|
||||
# Save another file with the same name.
|
||||
obj2 = Storage()
|
||||
obj2.normal.save("django_test.txt", ContentFile(b"more content"))
|
||||
obj2.normal.save("django_test.txt", ContentFile("more content"))
|
||||
self.assertEqual(obj2.normal.name, "tests/django_test_1.txt")
|
||||
self.assertEqual(obj2.normal.size, 12)
|
||||
|
||||
@@ -70,13 +70,13 @@ class FileStorageTests(TestCase):
|
||||
|
||||
# Deleting an object does not delete the file it uses.
|
||||
obj2.delete()
|
||||
obj2.normal.save("django_test.txt", ContentFile(b"more content"))
|
||||
obj2.normal.save("django_test.txt", ContentFile("more content"))
|
||||
self.assertEqual(obj2.normal.name, "tests/django_test_2.txt")
|
||||
|
||||
# Multiple files with the same name get _N appended to them.
|
||||
objs = [Storage() for i in range(3)]
|
||||
for o in objs:
|
||||
o.normal.save("multiple_files.txt", ContentFile(b"Same Content"))
|
||||
o.normal.save("multiple_files.txt", ContentFile("Same Content"))
|
||||
self.assertEqual(
|
||||
[o.normal.name for o in objs],
|
||||
["tests/multiple_files.txt", "tests/multiple_files_1.txt", "tests/multiple_files_2.txt"]
|
||||
@@ -100,7 +100,7 @@ class FileStorageTests(TestCase):
|
||||
# Verify the fix for #5655, making sure the directory is only
|
||||
# determined once.
|
||||
obj4 = Storage()
|
||||
obj4.random.save("random_file", ContentFile(b"random content"))
|
||||
obj4.random.save("random_file", ContentFile("random content"))
|
||||
self.assertTrue(obj4.random.name.endswith("/random_file"))
|
||||
|
||||
def test_max_length(self):
|
||||
@@ -135,6 +135,6 @@ class FileTests(unittest.TestCase):
|
||||
def test_file_mode(self):
|
||||
# Should not set mode to None if it is not present.
|
||||
# See #14681, stdlib gzip module crashes if mode is set to None
|
||||
file = SimpleUploadedFile("mode_test.txt", "content")
|
||||
file = SimpleUploadedFile("mode_test.txt", b"content")
|
||||
self.assertFalse(hasattr(file, 'mode'))
|
||||
g = gzip.GzipFile(fileobj=file)
|
||||
|
||||
@@ -12,38 +12,43 @@ from django.contrib.auth.models import Permission
|
||||
from django.contrib.contenttypes import generic
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Category(models.Model):
|
||||
title = models.CharField(max_length=100)
|
||||
description = models.TextField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
class Meta:
|
||||
ordering = ('title',)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100, default='Default headline')
|
||||
pub_date = models.DateTimeField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
class Meta:
|
||||
ordering = ('-pub_date', 'headline')
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Blog(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
featured = models.ForeignKey(Article, related_name='fixtures_featured_set')
|
||||
articles = models.ManyToManyField(Article, blank=True,
|
||||
related_name='fixtures_articles_set')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Tag(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
tagged_type = models.ForeignKey(ContentType, related_name="fixtures_tag_set")
|
||||
@@ -51,7 +56,7 @@ class Tag(models.Model):
|
||||
tagged = generic.GenericForeignKey(ct_field='tagged_type',
|
||||
fk_field='tagged_id')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '<%s: %s> tagged "%s"' % (self.tagged.__class__.__name__,
|
||||
self.tagged, self.name)
|
||||
|
||||
@@ -59,10 +64,11 @@ class PersonManager(models.Manager):
|
||||
def get_by_natural_key(self, name):
|
||||
return self.get(name=name)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
objects = PersonManager()
|
||||
name = models.CharField(max_length=100)
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Meta:
|
||||
@@ -79,19 +85,21 @@ class Spy(Person):
|
||||
objects = SpyManager()
|
||||
cover_blown = models.BooleanField(default=False)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Visa(models.Model):
|
||||
person = models.ForeignKey(Person)
|
||||
permissions = models.ManyToManyField(Permission, blank=True)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '%s %s' % (self.person.name,
|
||||
', '.join(p.name for p in self.permissions.all()))
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Book(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
authors = models.ManyToManyField(Person)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
authors = ' and '.join(a.name for a in self.authors.all())
|
||||
return '%s by %s' % (self.name, authors) if authors else self.name
|
||||
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100, default='Default headline')
|
||||
pub_date = models.DateTimeField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
class Meta:
|
||||
|
||||
@@ -14,8 +14,10 @@ from __future__ import unicode_literals
|
||||
from django.contrib.contenttypes import generic
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class TaggedItem(models.Model):
|
||||
"""A tag on an item."""
|
||||
tag = models.SlugField()
|
||||
@@ -27,12 +29,13 @@ class TaggedItem(models.Model):
|
||||
class Meta:
|
||||
ordering = ["tag", "content_type__name"]
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.tag
|
||||
|
||||
class ValuableTaggedItem(TaggedItem):
|
||||
value = models.PositiveIntegerField()
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Comparison(models.Model):
|
||||
"""
|
||||
A model that tests having multiple GenericForeignKeys
|
||||
@@ -48,9 +51,10 @@ class Comparison(models.Model):
|
||||
first_obj = generic.GenericForeignKey(ct_field="content_type1", fk_field="object_id1")
|
||||
other_obj = generic.GenericForeignKey(ct_field="content_type2", fk_field="object_id2")
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s is %s than %s" % (self.first_obj, self.comparative, self.other_obj)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Animal(models.Model):
|
||||
common_name = models.CharField(max_length=150)
|
||||
latin_name = models.CharField(max_length=150)
|
||||
@@ -60,25 +64,27 @@ class Animal(models.Model):
|
||||
object_id_field="object_id1",
|
||||
content_type_field="content_type1")
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.common_name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Vegetable(models.Model):
|
||||
name = models.CharField(max_length=150)
|
||||
is_yucky = models.BooleanField(default=True)
|
||||
|
||||
tags = generic.GenericRelation(TaggedItem)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Mineral(models.Model):
|
||||
name = models.CharField(max_length=150)
|
||||
hardness = models.PositiveSmallIntegerField()
|
||||
|
||||
# note the lack of an explicit GenericRelation here...
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class GeckoManager(models.Manager):
|
||||
|
||||
@@ -9,8 +9,10 @@ farthest into the future."
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100)
|
||||
pub_date = models.DateField()
|
||||
@@ -18,14 +20,15 @@ class Article(models.Model):
|
||||
class Meta:
|
||||
get_latest_by = 'pub_date'
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
name = models.CharField(max_length=30)
|
||||
birthday = models.DateField()
|
||||
|
||||
# Note that this model doesn't have "get_latest_by" set.
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@@ -11,23 +11,26 @@ performing a ``filter()`` lookup and raising a ``Http404`` exception if a
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Author(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class ArticleManager(models.Manager):
|
||||
def get_query_set(self):
|
||||
return super(ArticleManager, self).get_query_set().filter(authors__name__icontains='sir')
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
authors = models.ManyToManyField(Author)
|
||||
title = models.CharField(max_length=50)
|
||||
objects = models.Manager()
|
||||
by_a_sir = ArticleManager()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
@@ -9,14 +9,16 @@ parameters.
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
first_name = models.CharField(max_length=100)
|
||||
last_name = models.CharField(max_length=100)
|
||||
birthday = models.DateField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '%s %s' % (self.first_name, self.last_name)
|
||||
|
||||
class ManualPrimaryKeyTest(models.Model):
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import copy
|
||||
import sys
|
||||
from io import BytesIO
|
||||
|
||||
from django.core.management.validation import get_validation_errors
|
||||
from django.db.models.loading import cache, load_app
|
||||
|
||||
from django.utils import unittest
|
||||
from django.utils.six import StringIO
|
||||
|
||||
|
||||
class InvalidModelTestCase(unittest.TestCase):
|
||||
@@ -16,7 +16,7 @@ class InvalidModelTestCase(unittest.TestCase):
|
||||
# coloring attached (makes matching the results easier). We restore
|
||||
# sys.stderr afterwards.
|
||||
self.old_stdout = sys.stdout
|
||||
self.stdout = BytesIO()
|
||||
self.stdout = StringIO()
|
||||
sys.stdout = self.stdout
|
||||
|
||||
# This test adds dummy applications to the app cache. These
|
||||
|
||||
@@ -8,6 +8,7 @@ from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils import six
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
class Author(models.Model):
|
||||
@@ -15,6 +16,7 @@ class Author(models.Model):
|
||||
class Meta:
|
||||
ordering = ('name', )
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100)
|
||||
pub_date = models.DateTimeField()
|
||||
@@ -22,7 +24,7 @@ class Article(models.Model):
|
||||
class Meta:
|
||||
ordering = ('-pub_date', 'headline')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
class Tag(models.Model):
|
||||
@@ -31,24 +33,27 @@ class Tag(models.Model):
|
||||
class Meta:
|
||||
ordering = ('name', )
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Season(models.Model):
|
||||
year = models.PositiveSmallIntegerField()
|
||||
gt = models.IntegerField(null=True, blank=True)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return six.text_type(self.year)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Game(models.Model):
|
||||
season = models.ForeignKey(Season, related_name='games')
|
||||
home = models.CharField(max_length=100)
|
||||
away = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s at %s" % (self.away, self.home)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Player(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
games = models.ManyToManyField(Game, related_name='players')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@@ -7,17 +7,19 @@ from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils import six
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
class User(models.Model):
|
||||
username = models.CharField(max_length=20)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Issue(models.Model):
|
||||
num = models.IntegerField()
|
||||
cc = models.ManyToManyField(User, blank=True, related_name='test_issue_cc')
|
||||
client = models.ForeignKey(User, related_name='test_issue_client')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return six.text_type(self.num)
|
||||
|
||||
class Meta:
|
||||
|
||||
@@ -12,27 +12,31 @@ field, which specifies the ``Reporter``'s position for the given article
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Reporter(models.Model):
|
||||
first_name = models.CharField(max_length=30)
|
||||
last_name = models.CharField(max_length=30)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s %s" % (self.first_name, self.last_name)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100)
|
||||
pub_date = models.DateField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Writer(models.Model):
|
||||
reporter = models.ForeignKey(Reporter)
|
||||
article = models.ForeignKey(Article)
|
||||
position = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '%s (%s)' % (self.reporter, self.position)
|
||||
|
||||
|
||||
@@ -8,16 +8,19 @@ Set ``related_name`` to designate what the reverse relationship is called.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Category(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=50)
|
||||
pub_date = models.DateTimeField()
|
||||
@@ -26,6 +29,6 @@ class Article(models.Model):
|
||||
class Meta:
|
||||
ordering = ('pub_date',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
|
||||
@@ -17,12 +17,14 @@ appropriate.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
friends = models.ManyToManyField('self')
|
||||
idols = models.ManyToManyField('self', symmetrical=False, related_name='stalkers')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Part(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Car(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
default_parts = models.ManyToManyField(Part)
|
||||
@@ -18,12 +21,13 @@ class Car(models.Model):
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class SportsCar(Car):
|
||||
price = models.IntegerField()
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
fans = models.ManyToManyField('self', related_name='idols', symmetrical=False)
|
||||
@@ -32,5 +36,5 @@ class Person(models.Model):
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@@ -1,18 +1,21 @@
|
||||
from datetime import datetime
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
# M2M described on one of the models
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
name = models.CharField(max_length=128)
|
||||
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Group(models.Model):
|
||||
name = models.CharField(max_length=128)
|
||||
members = models.ManyToManyField(Person, through='Membership')
|
||||
@@ -22,9 +25,10 @@ class Group(models.Model):
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Membership(models.Model):
|
||||
person = models.ForeignKey(Person)
|
||||
group = models.ForeignKey(Group)
|
||||
@@ -34,16 +38,17 @@ class Membership(models.Model):
|
||||
class Meta:
|
||||
ordering = ('date_joined', 'invite_reason', 'group')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s is a member of %s" % (self.person.name, self.group.name)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class CustomMembership(models.Model):
|
||||
person = models.ForeignKey(Person, db_column="custom_person_column", related_name="custom_person_related_name")
|
||||
group = models.ForeignKey(Group)
|
||||
weird_fk = models.ForeignKey(Membership, null=True)
|
||||
date_joined = models.DateTimeField(default=datetime.now)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s is a member of %s" % (self.person.name, self.group.name)
|
||||
|
||||
class Meta:
|
||||
@@ -54,11 +59,12 @@ class TestNoDefaultsOrNulls(models.Model):
|
||||
group = models.ForeignKey(Group)
|
||||
nodefaultnonull = models.CharField(max_length=5)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class PersonSelfRefM2M(models.Model):
|
||||
name = models.CharField(max_length=5)
|
||||
friends = models.ManyToManyField('self', through="Friendship", symmetrical=False)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Friendship(models.Model):
|
||||
|
||||
@@ -11,19 +11,22 @@ Set ``related_name`` to designate what the reverse relationship is called.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Category(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
parent = models.ForeignKey('self', blank=True, null=True, related_name='child_set')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
full_name = models.CharField(max_length=20)
|
||||
mother = models.ForeignKey('self', null=True, related_name='mothers_child_set')
|
||||
father = models.ForeignKey('self', null=True, related_name='fathers_child_set')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.full_name
|
||||
|
||||
@@ -8,22 +8,25 @@ objects, and a ``Publication`` has multiple ``Article`` objects.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Publication(models.Model):
|
||||
title = models.CharField(max_length=30)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
class Meta:
|
||||
ordering = ('title',)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100)
|
||||
publications = models.ManyToManyField(Publication)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
class Meta:
|
||||
|
||||
@@ -6,22 +6,25 @@ To define a many-to-one relationship, use ``ForeignKey()``.
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Reporter(models.Model):
|
||||
first_name = models.CharField(max_length=30)
|
||||
last_name = models.CharField(max_length=30)
|
||||
email = models.EmailField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s %s" % (self.first_name, self.last_name)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100)
|
||||
pub_date = models.DateField()
|
||||
reporter = models.ForeignKey(Reporter)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
class Meta:
|
||||
|
||||
@@ -6,14 +6,17 @@ To define a many-to-one relationship that can have a null foreign key, use
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Reporter(models.Model):
|
||||
name = models.CharField(max_length=30)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100)
|
||||
reporter = models.ForeignKey(Reporter, null=True)
|
||||
@@ -21,5 +24,5 @@ class Article(models.Model):
|
||||
class Meta:
|
||||
ordering = ('headline',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
@@ -14,6 +14,7 @@ import tempfile
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
from django.db import models
|
||||
from django.utils import six
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
temp_storage_dir = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
|
||||
@@ -31,23 +32,29 @@ ARTICLE_STATUS_CHAR = (
|
||||
('l', 'Live'),
|
||||
)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Category(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
slug = models.SlugField(max_length=20)
|
||||
url = models.CharField('The URL', max_length=40)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Writer(models.Model):
|
||||
name = models.CharField(max_length=50, help_text='Use both first and last names.')
|
||||
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=50)
|
||||
slug = models.SlugField()
|
||||
@@ -64,7 +71,7 @@ class Article(models.Model):
|
||||
self.created = datetime.date.today()
|
||||
return super(Article, self).save()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
class ImprovedArticle(models.Model):
|
||||
@@ -76,26 +83,29 @@ class ImprovedArticleWithParentLink(models.Model):
|
||||
class BetterWriter(Writer):
|
||||
score = models.IntegerField()
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class WriterProfile(models.Model):
|
||||
writer = models.OneToOneField(Writer, primary_key=True)
|
||||
age = models.PositiveIntegerField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s is %s" % (self.writer, self.age)
|
||||
|
||||
from django.contrib.localflavor.us.models import PhoneNumberField
|
||||
@python_2_unicode_compatible
|
||||
class PhoneNumber(models.Model):
|
||||
phone = PhoneNumberField()
|
||||
description = models.CharField(max_length=20)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.phone
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class TextFile(models.Model):
|
||||
description = models.CharField(max_length=20)
|
||||
file = models.FileField(storage=temp_storage, upload_to='tests', max_length=15)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.description
|
||||
|
||||
try:
|
||||
@@ -110,6 +120,7 @@ try:
|
||||
|
||||
test_images = True
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class ImageFile(models.Model):
|
||||
def custom_upload_path(self, filename):
|
||||
path = self.path or 'tests'
|
||||
@@ -125,9 +136,10 @@ try:
|
||||
width_field='width', height_field='height')
|
||||
path = models.CharField(max_length=16, blank=True, default='')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.description
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class OptionalImageFile(models.Model):
|
||||
def custom_upload_path(self, filename):
|
||||
path = self.path or 'tests'
|
||||
@@ -141,28 +153,31 @@ try:
|
||||
height = models.IntegerField(editable=False, null=True)
|
||||
path = models.CharField(max_length=16, blank=True, default='')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.description
|
||||
except ImportError:
|
||||
test_images = False
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class CommaSeparatedInteger(models.Model):
|
||||
field = models.CommaSeparatedIntegerField(max_length=20)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.field
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Product(models.Model):
|
||||
slug = models.SlugField(unique=True)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.slug
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Price(models.Model):
|
||||
price = models.DecimalField(max_digits=10, decimal_places=2)
|
||||
quantity = models.PositiveIntegerField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s for %s" % (self.quantity, self.price)
|
||||
|
||||
class Meta:
|
||||
@@ -171,6 +186,7 @@ class Price(models.Model):
|
||||
class ArticleStatus(models.Model):
|
||||
status = models.CharField(max_length=2, choices=ARTICLE_STATUS_CHAR, blank=True, null=True)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Inventory(models.Model):
|
||||
barcode = models.PositiveIntegerField(unique=True)
|
||||
parent = models.ForeignKey('self', to_field='barcode', blank=True, null=True)
|
||||
@@ -179,9 +195,12 @@ class Inventory(models.Model):
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
class Book(models.Model):
|
||||
title = models.CharField(max_length=40)
|
||||
author = models.ForeignKey(Writer, blank=True, null=True)
|
||||
@@ -202,31 +221,34 @@ class BookXtra(models.Model):
|
||||
class DerivedBook(Book, BookXtra):
|
||||
pass
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class ExplicitPK(models.Model):
|
||||
key = models.CharField(max_length=20, primary_key=True)
|
||||
desc = models.CharField(max_length=20, blank=True, unique=True)
|
||||
class Meta:
|
||||
unique_together = ('key', 'desc')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.key
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Post(models.Model):
|
||||
title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
|
||||
slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
|
||||
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
|
||||
posted = models.DateField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class DerivedPost(Post):
|
||||
pass
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class BigInt(models.Model):
|
||||
biggie = models.BigIntegerField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return six.text_type(self.biggie)
|
||||
|
||||
class MarkupField(models.CharField):
|
||||
|
||||
@@ -179,14 +179,14 @@ class PriceFormWithoutQuantity(forms.ModelForm):
|
||||
|
||||
class ModelFormBaseTest(TestCase):
|
||||
def test_base_form(self):
|
||||
self.assertEqual(BaseCategoryForm.base_fields.keys(),
|
||||
self.assertEqual(list(BaseCategoryForm.base_fields),
|
||||
['name', 'slug', 'url'])
|
||||
|
||||
def test_extra_fields(self):
|
||||
class ExtraFields(BaseCategoryForm):
|
||||
some_extra_field = forms.BooleanField()
|
||||
|
||||
self.assertEqual(ExtraFields.base_fields.keys(),
|
||||
self.assertEqual(list(ExtraFields.base_fields),
|
||||
['name', 'slug', 'url', 'some_extra_field'])
|
||||
|
||||
def test_replace_field(self):
|
||||
@@ -215,7 +215,7 @@ class ModelFormBaseTest(TestCase):
|
||||
model = Category
|
||||
fields = ['url']
|
||||
|
||||
self.assertEqual(LimitFields.base_fields.keys(),
|
||||
self.assertEqual(list(LimitFields.base_fields),
|
||||
['url'])
|
||||
|
||||
def test_exclude_fields(self):
|
||||
@@ -224,7 +224,7 @@ class ModelFormBaseTest(TestCase):
|
||||
model = Category
|
||||
exclude = ['url']
|
||||
|
||||
self.assertEqual(ExcludeFields.base_fields.keys(),
|
||||
self.assertEqual(list(ExcludeFields.base_fields),
|
||||
['name', 'slug'])
|
||||
|
||||
def test_confused_form(self):
|
||||
@@ -237,7 +237,7 @@ class ModelFormBaseTest(TestCase):
|
||||
fields = ['name', 'url']
|
||||
exclude = ['url']
|
||||
|
||||
self.assertEqual(ConfusedForm.base_fields.keys(),
|
||||
self.assertEqual(list(ConfusedForm.base_fields),
|
||||
['name'])
|
||||
|
||||
def test_mixmodel_form(self):
|
||||
@@ -254,13 +254,13 @@ class ModelFormBaseTest(TestCase):
|
||||
# overrides BaseCategoryForm.Meta.
|
||||
|
||||
self.assertEqual(
|
||||
MixModelForm.base_fields.keys(),
|
||||
list(MixModelForm.base_fields),
|
||||
['headline', 'slug', 'pub_date', 'writer', 'article', 'categories', 'status']
|
||||
)
|
||||
|
||||
def test_article_form(self):
|
||||
self.assertEqual(
|
||||
ArticleForm.base_fields.keys(),
|
||||
list(ArticleForm.base_fields),
|
||||
['headline', 'slug', 'pub_date', 'writer', 'article', 'categories', 'status']
|
||||
)
|
||||
|
||||
@@ -270,7 +270,7 @@ class ModelFormBaseTest(TestCase):
|
||||
pass
|
||||
|
||||
self.assertEqual(
|
||||
BadForm.base_fields.keys(),
|
||||
list(BadForm.base_fields),
|
||||
['headline', 'slug', 'pub_date', 'writer', 'article', 'categories', 'status']
|
||||
)
|
||||
|
||||
@@ -282,7 +282,7 @@ class ModelFormBaseTest(TestCase):
|
||||
"""
|
||||
pass
|
||||
|
||||
self.assertEqual(SubCategoryForm.base_fields.keys(),
|
||||
self.assertEqual(list(SubCategoryForm.base_fields),
|
||||
['name', 'slug', 'url'])
|
||||
|
||||
def test_subclassmeta_form(self):
|
||||
@@ -312,7 +312,7 @@ class ModelFormBaseTest(TestCase):
|
||||
model = Category
|
||||
fields = ['url', 'name']
|
||||
|
||||
self.assertEqual(OrderFields.base_fields.keys(),
|
||||
self.assertEqual(list(OrderFields.base_fields),
|
||||
['url', 'name'])
|
||||
self.assertHTMLEqual(
|
||||
str(OrderFields()),
|
||||
@@ -327,7 +327,7 @@ class ModelFormBaseTest(TestCase):
|
||||
fields = ['slug', 'url', 'name']
|
||||
exclude = ['url']
|
||||
|
||||
self.assertEqual(OrderFields2.base_fields.keys(),
|
||||
self.assertEqual(list(OrderFields2.base_fields),
|
||||
['slug', 'name'])
|
||||
|
||||
|
||||
@@ -751,9 +751,9 @@ class OldFormForXTests(TestCase):
|
||||
self.assertEqual(new_art.headline, 'New headline')
|
||||
|
||||
# Add some categories and test the many-to-many form output.
|
||||
self.assertEqual(map(lambda o: o.name, new_art.categories.all()), [])
|
||||
self.assertQuerysetEqual(new_art.categories.all(), [])
|
||||
new_art.categories.add(Category.objects.get(name='Entertainment'))
|
||||
self.assertEqual(map(lambda o: o.name, new_art.categories.all()), ["Entertainment"])
|
||||
self.assertQuerysetEqual(new_art.categories.all(), ["Entertainment"])
|
||||
f = TestArticleForm(auto_id=False, instance=new_art)
|
||||
self.assertHTMLEqual(f.as_ul(), '''<li>Headline: <input type="text" name="headline" value="New headline" maxlength="50" /></li>
|
||||
<li>Slug: <input type="text" name="slug" value="new-headline" maxlength="50" /></li>
|
||||
@@ -815,7 +815,7 @@ class OldFormForXTests(TestCase):
|
||||
new_art = f.save()
|
||||
self.assertEqual(new_art.id == art_id_1, True)
|
||||
new_art = Article.objects.get(id=art_id_1)
|
||||
self.assertEqual(map(lambda o: o.name, new_art.categories.order_by('name')),
|
||||
self.assertQuerysetEqual(new_art.categories.order_by('name'),
|
||||
["Entertainment", "It's a test"])
|
||||
|
||||
# Now, submit form data with no categories. This deletes the existing categories.
|
||||
@@ -824,7 +824,7 @@ class OldFormForXTests(TestCase):
|
||||
new_art = f.save()
|
||||
self.assertEqual(new_art.id == art_id_1, True)
|
||||
new_art = Article.objects.get(id=art_id_1)
|
||||
self.assertEqual(map(lambda o: o.name, new_art.categories.all()), [])
|
||||
self.assertQuerysetEqual(new_art.categories.all(), [])
|
||||
|
||||
# Create a new article, with categories, via the form.
|
||||
f = ArticleForm({'headline': 'The walrus was Paul', 'slug': 'walrus-was-paul', 'pub_date': '1967-11-01',
|
||||
@@ -833,7 +833,7 @@ class OldFormForXTests(TestCase):
|
||||
art_id_2 = new_art.id
|
||||
self.assertEqual(art_id_2 not in (None, art_id_1), True)
|
||||
new_art = Article.objects.get(id=art_id_2)
|
||||
self.assertEqual(map(lambda o: o.name, new_art.categories.order_by('name')), ["Entertainment", "It's a test"])
|
||||
self.assertQuerysetEqual(new_art.categories.order_by('name'), ["Entertainment", "It's a test"])
|
||||
|
||||
# Create a new article, with no categories, via the form.
|
||||
f = ArticleForm({'headline': 'The walrus was Paul', 'slug': 'walrus-was-paul', 'pub_date': '1967-11-01',
|
||||
@@ -842,7 +842,7 @@ class OldFormForXTests(TestCase):
|
||||
art_id_3 = new_art.id
|
||||
self.assertEqual(art_id_3 not in (None, art_id_1, art_id_2), True)
|
||||
new_art = Article.objects.get(id=art_id_3)
|
||||
self.assertEqual(map(lambda o: o.name, new_art.categories.all()), [])
|
||||
self.assertQuerysetEqual(new_art.categories.all(), [])
|
||||
|
||||
# Create a new article, with categories, via the form, but use commit=False.
|
||||
# The m2m data won't be saved until save_m2m() is invoked on the form.
|
||||
@@ -857,11 +857,11 @@ class OldFormForXTests(TestCase):
|
||||
|
||||
# The instance doesn't have m2m data yet
|
||||
new_art = Article.objects.get(id=art_id_4)
|
||||
self.assertEqual(map(lambda o: o.name, new_art.categories.all()), [])
|
||||
self.assertQuerysetEqual(new_art.categories.all(), [])
|
||||
|
||||
# Save the m2m data on the form
|
||||
f.save_m2m()
|
||||
self.assertEqual(map(lambda o: o.name, new_art.categories.order_by('name')), ["Entertainment", "It's a test"])
|
||||
self.assertQuerysetEqual(new_art.categories.order_by('name'), ["Entertainment", "It's a test"])
|
||||
|
||||
# Here, we define a custom ModelForm. Because it happens to have the same fields as
|
||||
# the Category model, we can just call the form's save() to apply its changes to an
|
||||
@@ -1007,12 +1007,12 @@ class OldFormForXTests(TestCase):
|
||||
f.clean(None)
|
||||
with self.assertRaises(ValidationError):
|
||||
f.clean([])
|
||||
self.assertEqual(map(lambda o: o.name, f.clean([c1.id])), ["Entertainment"])
|
||||
self.assertEqual(map(lambda o: o.name, f.clean([c2.id])), ["It's a test"])
|
||||
self.assertEqual(map(lambda o: o.name, f.clean([str(c1.id)])), ["Entertainment"])
|
||||
self.assertEqual(map(lambda o: o.name, f.clean([str(c1.id), str(c2.id)])), ["Entertainment", "It's a test"])
|
||||
self.assertEqual(map(lambda o: o.name, f.clean([c1.id, str(c2.id)])), ["Entertainment", "It's a test"])
|
||||
self.assertEqual(map(lambda o: o.name, f.clean((c1.id, str(c2.id)))), ["Entertainment", "It's a test"])
|
||||
self.assertQuerysetEqual(f.clean([c1.id]), ["Entertainment"])
|
||||
self.assertQuerysetEqual(f.clean([c2.id]), ["It's a test"])
|
||||
self.assertQuerysetEqual(f.clean([str(c1.id)]), ["Entertainment"])
|
||||
self.assertQuerysetEqual(f.clean([str(c1.id), str(c2.id)]), ["Entertainment", "It's a test"])
|
||||
self.assertQuerysetEqual(f.clean([c1.id, str(c2.id)]), ["Entertainment", "It's a test"])
|
||||
self.assertQuerysetEqual(f.clean((c1.id, str(c2.id))), ["Entertainment", "It's a test"])
|
||||
with self.assertRaises(ValidationError):
|
||||
f.clean(['100'])
|
||||
with self.assertRaises(ValidationError):
|
||||
@@ -1025,7 +1025,7 @@ class OldFormForXTests(TestCase):
|
||||
# than caching it at time of instantiation.
|
||||
c6 = Category.objects.create(id=6, name='Sixth', url='6th')
|
||||
self.assertEqual(c6.name, 'Sixth')
|
||||
self.assertEqual(map(lambda o: o.name, f.clean([c6.id])), ["Sixth"])
|
||||
self.assertQuerysetEqual(f.clean([c6.id]), ["Sixth"])
|
||||
|
||||
# Delete a Category object *after* the ModelMultipleChoiceField has already been
|
||||
# instantiated. This proves clean() checks the database during clean() rather
|
||||
@@ -1050,7 +1050,7 @@ class OldFormForXTests(TestCase):
|
||||
(c1.pk, 'Entertainment'),
|
||||
(c2.pk, "It's a test"),
|
||||
(c3.pk, 'Third')])
|
||||
self.assertEqual(map(lambda o: o.name, f.clean([c3.id])), ["Third"])
|
||||
self.assertQuerysetEqual(f.clean([c3.id]), ["Third"])
|
||||
with self.assertRaises(ValidationError):
|
||||
f.clean([c4.id])
|
||||
with self.assertRaises(ValidationError):
|
||||
@@ -1066,13 +1066,13 @@ class OldFormForXTests(TestCase):
|
||||
|
||||
# OneToOneField ###############################################################
|
||||
|
||||
self.assertEqual(ImprovedArticleForm.base_fields.keys(), ['article'])
|
||||
self.assertEqual(list(ImprovedArticleForm.base_fields), ['article'])
|
||||
|
||||
self.assertEqual(ImprovedArticleWithParentLinkForm.base_fields.keys(), [])
|
||||
self.assertEqual(list(ImprovedArticleWithParentLinkForm.base_fields), [])
|
||||
|
||||
bw = BetterWriter(name='Joe Better', score=10)
|
||||
bw.save()
|
||||
self.assertEqual(sorted(model_to_dict(bw).keys()),
|
||||
self.assertEqual(sorted(model_to_dict(bw)),
|
||||
['id', 'name', 'score', 'writer_ptr'])
|
||||
|
||||
form = BetterWriterForm({'name': 'Some Name', 'score': 12})
|
||||
@@ -1463,7 +1463,7 @@ class OldFormForXTests(TestCase):
|
||||
model = Category
|
||||
fields = ['description', 'url']
|
||||
|
||||
self.assertEqual(CategoryForm.base_fields.keys(),
|
||||
self.assertEqual(list(CategoryForm.base_fields),
|
||||
['description', 'url'])
|
||||
|
||||
self.assertHTMLEqual(six.text_type(CategoryForm()), '''<tr><th><label for="id_description">Description:</label></th><td><input type="text" name="description" id="id_description" /></td></tr>
|
||||
@@ -1472,14 +1472,15 @@ class OldFormForXTests(TestCase):
|
||||
|
||||
field = forms.ModelMultipleChoiceField(Inventory.objects.all(), to_field_name='barcode')
|
||||
self.assertEqual(tuple(field.choices), ((86, 'Apple'), (87, 'Core'), (22, 'Pear')))
|
||||
self.assertEqual(map(lambda o: o.name, field.clean([86])), ['Apple'])
|
||||
self.assertQuerysetEqual(field.clean([86]), ['Apple'])
|
||||
|
||||
form = SelectInventoryForm({'items': [87, 22]})
|
||||
self.assertEqual(form.is_valid(), True)
|
||||
self.assertEqual(len(form.cleaned_data), 1)
|
||||
self.assertEqual(map(lambda o: o.name, form.cleaned_data['items']), ['Core', 'Pear'])
|
||||
self.assertQuerysetEqual(form.cleaned_data['items'], ['Core', 'Pear'])
|
||||
|
||||
def test_model_field_that_returns_none_to_exclude_itself_with_explicit_fields(self):
|
||||
self.assertEqual(CustomFieldForExclusionForm.base_fields.keys(), ['name'])
|
||||
self.assertEqual(list(CustomFieldForExclusionForm.base_fields),
|
||||
['name'])
|
||||
self.assertHTMLEqual(six.text_type(CustomFieldForExclusionForm()),
|
||||
'''<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="10" /></td></tr>''')
|
||||
|
||||
@@ -4,20 +4,23 @@ import datetime
|
||||
|
||||
from django.db import models
|
||||
from django.utils import six
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Author(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class BetterAuthor(Author):
|
||||
write_speed = models.IntegerField()
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Book(models.Model):
|
||||
author = models.ForeignKey(Author)
|
||||
title = models.CharField(max_length=100)
|
||||
@@ -28,20 +31,22 @@ class Book(models.Model):
|
||||
)
|
||||
ordering = ['id']
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class BookWithCustomPK(models.Model):
|
||||
my_pk = models.DecimalField(max_digits=5, decimal_places=0, primary_key=True)
|
||||
author = models.ForeignKey(Author)
|
||||
title = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '%s: %s' % (self.my_pk, self.title)
|
||||
|
||||
class Editor(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class BookWithOptionalAltEditor(models.Model):
|
||||
author = models.ForeignKey(Author)
|
||||
# Optional secondary author
|
||||
@@ -53,21 +58,23 @@ class BookWithOptionalAltEditor(models.Model):
|
||||
('author', 'title', 'alt_editor'),
|
||||
)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class AlternateBook(Book):
|
||||
notes = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '%s - %s' % (self.title, self.notes)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class AuthorMeeting(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
authors = models.ManyToManyField(Author)
|
||||
created = models.DateField(editable=False)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class CustomPrimaryKey(models.Model):
|
||||
@@ -77,19 +84,21 @@ class CustomPrimaryKey(models.Model):
|
||||
|
||||
# models for inheritance tests.
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Place(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
city = models.CharField(max_length=50)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Owner(models.Model):
|
||||
auto_id = models.AutoField(primary_key=True)
|
||||
name = models.CharField(max_length=100)
|
||||
place = models.ForeignKey(Place)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s at %s" % (self.name, self.place)
|
||||
|
||||
class Location(models.Model):
|
||||
@@ -98,30 +107,34 @@ class Location(models.Model):
|
||||
lat = models.CharField(max_length=100)
|
||||
lon = models.CharField(max_length=100)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class OwnerProfile(models.Model):
|
||||
owner = models.OneToOneField(Owner, primary_key=True)
|
||||
age = models.PositiveIntegerField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s is %d" % (self.owner.name, self.age)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Restaurant(Place):
|
||||
serves_pizza = models.BooleanField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Product(models.Model):
|
||||
slug = models.SlugField(unique=True)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.slug
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Price(models.Model):
|
||||
price = models.DecimalField(max_digits=10, decimal_places=2)
|
||||
quantity = models.PositiveIntegerField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s for %s" % (self.quantity, self.price)
|
||||
|
||||
class Meta:
|
||||
@@ -136,12 +149,14 @@ class ClassyMexicanRestaurant(MexicanRestaurant):
|
||||
|
||||
# models for testing unique_together validation when a fk is involved and
|
||||
# using inlineformset_factory.
|
||||
@python_2_unicode_compatible
|
||||
class Repository(models.Model):
|
||||
name = models.CharField(max_length=25)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Revision(models.Model):
|
||||
repository = models.ForeignKey(Repository)
|
||||
revision = models.CharField(max_length=40)
|
||||
@@ -149,7 +164,7 @@ class Revision(models.Model):
|
||||
class Meta:
|
||||
unique_together = (("repository", "revision"),)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s (%s)" % (self.revision, six.text_type(self.repository))
|
||||
|
||||
# models for testing callable defaults (see bug #7975). If you define a model
|
||||
@@ -167,32 +182,36 @@ class Membership(models.Model):
|
||||
class Team(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Player(models.Model):
|
||||
team = models.ForeignKey(Team, null=True)
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
# Models for testing custom ModelForm save methods in formsets and inline formsets
|
||||
@python_2_unicode_compatible
|
||||
class Poet(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Poem(models.Model):
|
||||
poet = models.ForeignKey(Poet)
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Post(models.Model):
|
||||
title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
|
||||
slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
|
||||
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
|
||||
posted = models.DateField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@@ -14,11 +14,13 @@ Both styles are demonstrated here.
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
#
|
||||
# Abstract base classes
|
||||
#
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class CommonInfo(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
age = models.PositiveIntegerField()
|
||||
@@ -27,7 +29,7 @@ class CommonInfo(models.Model):
|
||||
abstract = True
|
||||
ordering = ['name']
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '%s %s' % (self.__class__.__name__, self.name)
|
||||
|
||||
class Worker(CommonInfo):
|
||||
@@ -49,6 +51,7 @@ class StudentWorker(Student, Worker):
|
||||
class Post(models.Model):
|
||||
title = models.CharField(max_length=50)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Attachment(models.Model):
|
||||
post = models.ForeignKey(Post, related_name='attached_%(class)s_set')
|
||||
content = models.TextField()
|
||||
@@ -56,7 +59,7 @@ class Attachment(models.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.content
|
||||
|
||||
class Comment(Attachment):
|
||||
@@ -69,17 +72,19 @@ class Link(Attachment):
|
||||
# Multi-table inheritance
|
||||
#
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Chef(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s the chef" % self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Place(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
address = models.CharField(max_length=80)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s the place" % self.name
|
||||
|
||||
class Rating(models.Model):
|
||||
@@ -89,6 +94,7 @@ class Rating(models.Model):
|
||||
abstract = True
|
||||
ordering = ['-rating']
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Restaurant(Place, Rating):
|
||||
serves_hot_dogs = models.BooleanField()
|
||||
serves_pizza = models.BooleanField()
|
||||
@@ -97,27 +103,30 @@ class Restaurant(Place, Rating):
|
||||
class Meta(Rating.Meta):
|
||||
db_table = 'my_restaurant'
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s the restaurant" % self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class ItalianRestaurant(Restaurant):
|
||||
serves_gnocchi = models.BooleanField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s the italian restaurant" % self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Supplier(Place):
|
||||
customers = models.ManyToManyField(Restaurant, related_name='provider')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s the supplier" % self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class ParkingLot(Place):
|
||||
# An explicit link to the parent (we can control the attribute name).
|
||||
parent = models.OneToOneField(Place, primary_key=True, parent_link=True)
|
||||
main_site = models.ForeignKey(Place, related_name='lot')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s the parking lot" % self.name
|
||||
|
||||
#
|
||||
@@ -139,10 +148,11 @@ class NamedURL(models.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Copy(NamedURL):
|
||||
content = models.TextField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.content
|
||||
|
||||
class Mixin(object):
|
||||
|
||||
@@ -11,12 +11,14 @@ from __future__ import absolute_import
|
||||
from django.db import models
|
||||
|
||||
from ..model_inheritance.models import NamedURL
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
#
|
||||
# Abstract base classes with related models
|
||||
#
|
||||
@python_2_unicode_compatible
|
||||
class Copy(NamedURL):
|
||||
content = models.TextField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.content
|
||||
|
||||
@@ -8,28 +8,32 @@ In this example, a ``Place`` optionally can be a ``Restaurant``.
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Place(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
address = models.CharField(max_length=80)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s the place" % self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Restaurant(models.Model):
|
||||
place = models.OneToOneField(Place, primary_key=True)
|
||||
serves_hot_dogs = models.BooleanField()
|
||||
serves_pizza = models.BooleanField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s the restaurant" % self.place.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Waiter(models.Model):
|
||||
restaurant = models.ForeignKey(Restaurant)
|
||||
name = models.CharField(max_length=50)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s the waiter at %s" % (self.name, self.restaurant)
|
||||
|
||||
class ManualPrimaryKey(models.Model):
|
||||
@@ -40,10 +44,11 @@ class RelatedModel(models.Model):
|
||||
link = models.OneToOneField(ManualPrimaryKey)
|
||||
name = models.CharField(max_length = 50)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class MultiModel(models.Model):
|
||||
link1 = models.OneToOneField(Place)
|
||||
link2 = models.OneToOneField(ManualPrimaryKey)
|
||||
name = models.CharField(max_length=50)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "Multimodel %s" % self.name
|
||||
|
||||
@@ -10,8 +10,10 @@ clauses using the variable ``django.db.models.Q`` (or any object with an
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=50)
|
||||
pub_date = models.DateTimeField()
|
||||
@@ -19,5 +21,5 @@ class Article(models.Model):
|
||||
class Meta:
|
||||
ordering = ('pub_date',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
@@ -4,11 +4,13 @@ Tests for the order_with_respect_to Meta attribute.
|
||||
|
||||
from django.db import models
|
||||
from django.utils import six
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
class Question(models.Model):
|
||||
text = models.CharField(max_length=200)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Answer(models.Model):
|
||||
text = models.CharField(max_length=200)
|
||||
question = models.ForeignKey(Question)
|
||||
@@ -16,9 +18,10 @@ class Answer(models.Model):
|
||||
class Meta:
|
||||
order_with_respect_to = 'question'
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return six.text_type(self.text)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Post(models.Model):
|
||||
title = models.CharField(max_length=200)
|
||||
parent = models.ForeignKey("self", related_name="children", null=True)
|
||||
@@ -26,5 +29,5 @@ class Post(models.Model):
|
||||
class Meta:
|
||||
order_with_respect_to = "parent"
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
@@ -14,22 +14,25 @@ undefined -- not random, just undefined.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100)
|
||||
pub_date = models.DateTimeField()
|
||||
class Meta:
|
||||
ordering = ('-pub_date', 'headline')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class ArticlePKOrdering(models.Model):
|
||||
headline = models.CharField(max_length=100)
|
||||
pub_date = models.DateTimeField()
|
||||
class Meta:
|
||||
ordering = ('-pk',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
@@ -7,11 +7,13 @@ objects into easily readable pages.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
headline = models.CharField(max_length=100, default='Default headline')
|
||||
pub_date = models.DateTimeField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
@@ -28,7 +28,7 @@ class PaginationTests(TestCase):
|
||||
paginator = Paginator(Article.objects.all(), 5)
|
||||
self.assertEqual(9, paginator.count)
|
||||
self.assertEqual(2, paginator.num_pages)
|
||||
self.assertEqual([1, 2], paginator.page_range)
|
||||
self.assertEqual([1, 2], list(paginator.page_range))
|
||||
|
||||
def test_first_page(self):
|
||||
paginator = Paginator(Article.objects.all(), 5)
|
||||
@@ -78,13 +78,13 @@ class PaginationTests(TestCase):
|
||||
paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=True)
|
||||
self.assertEqual(0, paginator.count)
|
||||
self.assertEqual(1, paginator.num_pages)
|
||||
self.assertEqual([1], paginator.page_range)
|
||||
self.assertEqual([1], list(paginator.page_range))
|
||||
|
||||
# Empty paginators with allow_empty_first_page=False.
|
||||
paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=False)
|
||||
self.assertEqual(0, paginator.count)
|
||||
self.assertEqual(0, paginator.num_pages)
|
||||
self.assertEqual([], paginator.page_range)
|
||||
self.assertEqual([], list(paginator.page_range))
|
||||
|
||||
def test_invalid_page(self):
|
||||
paginator = Paginator(Article.objects.all(), 5)
|
||||
@@ -108,7 +108,7 @@ class PaginationTests(TestCase):
|
||||
paginator = Paginator([1, 2, 3, 4, 5, 6, 7, 8, 9], 3)
|
||||
self.assertEqual(9, paginator.count)
|
||||
self.assertEqual(3, paginator.num_pages)
|
||||
self.assertEqual([1, 2, 3], paginator.page_range)
|
||||
self.assertEqual([1, 2, 3], list(paginator.page_range))
|
||||
p = paginator.page(2)
|
||||
self.assertEqual("<Page 2 of 3>", six.text_type(p))
|
||||
self.assertEqual([4, 5, 6], p.object_list)
|
||||
@@ -125,10 +125,10 @@ class PaginationTests(TestCase):
|
||||
paginator = Paginator(CountContainer(), 10)
|
||||
self.assertEqual(42, paginator.count)
|
||||
self.assertEqual(5, paginator.num_pages)
|
||||
self.assertEqual([1, 2, 3, 4, 5], paginator.page_range)
|
||||
self.assertEqual([1, 2, 3, 4, 5], list(paginator.page_range))
|
||||
|
||||
# Paginator can be passed other objects that implement __len__.
|
||||
paginator = Paginator(LenContainer(), 10)
|
||||
self.assertEqual(42, paginator.count)
|
||||
self.assertEqual(5, paginator.num_pages)
|
||||
self.assertEqual([1, 2, 3, 4, 5], paginator.page_range)
|
||||
self.assertEqual([1, 2, 3, 4, 5], list(paginator.page_range))
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
from django.contrib.contenttypes import generic
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
## Basic tests
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Author(models.Model):
|
||||
name = models.CharField(max_length=50, unique=True)
|
||||
first_book = models.ForeignKey('Book', related_name='first_time_authors')
|
||||
favorite_authors = models.ManyToManyField(
|
||||
'self', through='FavoriteAuthors', symmetrical=False, related_name='favors_me')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Meta:
|
||||
@@ -30,6 +32,7 @@ class FavoriteAuthors(models.Model):
|
||||
ordering = ['id']
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class AuthorAddress(models.Model):
|
||||
author = models.ForeignKey(Author, to_field='name', related_name='addresses')
|
||||
address = models.TextField()
|
||||
@@ -37,15 +40,16 @@ class AuthorAddress(models.Model):
|
||||
class Meta:
|
||||
ordering = ['id']
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.address
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Book(models.Model):
|
||||
title = models.CharField(max_length=255)
|
||||
authors = models.ManyToManyField(Author, related_name='books')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
class Meta:
|
||||
@@ -58,11 +62,12 @@ class BookWithYear(Book):
|
||||
AuthorWithAge, related_name='books_with_year')
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Reader(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
books_read = models.ManyToManyField(Book, related_name='read_by')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Meta:
|
||||
@@ -86,13 +91,14 @@ class TeacherManager(models.Manager):
|
||||
return super(TeacherManager, self).get_query_set().prefetch_related('qualifications')
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Teacher(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
qualifications = models.ManyToManyField(Qualification)
|
||||
|
||||
objects = TeacherManager()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s (%s)" % (self.name, ", ".join(q.name for q in self.qualifications.all()))
|
||||
|
||||
class Meta:
|
||||
@@ -109,6 +115,7 @@ class Department(models.Model):
|
||||
|
||||
## GenericRelation/GenericForeignKey tests
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class TaggedItem(models.Model):
|
||||
tag = models.SlugField()
|
||||
content_type = models.ForeignKey(ContentType, related_name="taggeditem_set2")
|
||||
@@ -119,7 +126,7 @@ class TaggedItem(models.Model):
|
||||
created_by_fkey = models.PositiveIntegerField(null=True)
|
||||
created_by = generic.GenericForeignKey('created_by_ct', 'created_by_fkey',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.tag
|
||||
|
||||
|
||||
@@ -169,12 +176,13 @@ class Person(models.Model):
|
||||
|
||||
## Models for nullable FK tests
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Employee(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
boss = models.ForeignKey('self', null=True,
|
||||
related_name='serfs')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Meta:
|
||||
|
||||
@@ -352,7 +352,7 @@ class MultiTableInheritanceTest(TestCase):
|
||||
with self.assertNumQueries(2):
|
||||
qs = BookReview.objects.prefetch_related('book')
|
||||
titles = [obj.book.title for obj in qs]
|
||||
self.assertEquals(titles, ["Poems", "More poems"])
|
||||
self.assertEqual(titles, ["Poems", "More poems"])
|
||||
|
||||
def test_m2m_to_inheriting_model(self):
|
||||
qs = AuthorWithAge.objects.prefetch_related('books_with_year')
|
||||
|
||||
@@ -24,7 +24,8 @@ class ProxyModelInheritanceTests(TransactionTestCase):
|
||||
def setUp(self):
|
||||
self.old_sys_path = sys.path[:]
|
||||
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
||||
map(load_app, settings.INSTALLED_APPS)
|
||||
for app in settings.INSTALLED_APPS:
|
||||
load_app(app)
|
||||
|
||||
def tearDown(self):
|
||||
sys.path = self.old_sys_path
|
||||
|
||||
@@ -5,6 +5,7 @@ than using a new table of their own. This allows them to act as simple proxies,
|
||||
providing a modified interface to the data from the base class.
|
||||
"""
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
# A couple of managers for testing managing overriding in proxy model cases.
|
||||
|
||||
@@ -16,6 +17,7 @@ class SubManager(models.Manager):
|
||||
def get_query_set(self):
|
||||
return super(SubManager, self).get_query_set().exclude(name="wilma")
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
"""
|
||||
A simple concrete base class.
|
||||
@@ -24,7 +26,7 @@ class Person(models.Model):
|
||||
|
||||
objects = PersonManager()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Abstract(models.Model):
|
||||
@@ -82,10 +84,11 @@ class MyPersonProxy(MyPerson):
|
||||
class LowerStatusPerson(MyPersonProxy):
|
||||
status = models.CharField(max_length=80)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class User(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class UserProxy(User):
|
||||
@@ -100,11 +103,12 @@ class UserProxyProxy(UserProxy):
|
||||
class Country(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class State(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
country = models.ForeignKey(Country)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class StateProxy(State):
|
||||
@@ -124,11 +128,12 @@ class ProxyTrackerUser(TrackerUser):
|
||||
proxy = True
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Issue(models.Model):
|
||||
summary = models.CharField(max_length=255)
|
||||
assignee = models.ForeignKey(TrackerUser)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return ':'.join((self.__class__.__name__,self.summary,))
|
||||
|
||||
class Bug(Issue):
|
||||
|
||||
@@ -8,8 +8,10 @@ reserved-name usage.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Thing(models.Model):
|
||||
when = models.CharField(max_length=1, primary_key=True)
|
||||
join = models.CharField(max_length=1)
|
||||
@@ -22,5 +24,5 @@ class Thing(models.Model):
|
||||
class Meta:
|
||||
db_table = 'select'
|
||||
|
||||
def __unicode__(self):
|
||||
return self.when
|
||||
def __str__(self):
|
||||
return self.when
|
||||
|
||||
@@ -5,25 +5,29 @@ This demonstrates the reverse lookup features of the database API.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class User(models.Model):
|
||||
name = models.CharField(max_length=200)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Poll(models.Model):
|
||||
question = models.CharField(max_length=200)
|
||||
creator = models.ForeignKey(User)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.question
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Choice(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
poll = models.ForeignKey(Poll, related_name="poll_choice")
|
||||
related_poll = models.ForeignKey(Poll, related_name="related_choice")
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@@ -7,8 +7,10 @@ the methods.
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
first_name = models.CharField(max_length=20)
|
||||
last_name = models.CharField(max_length=20)
|
||||
@@ -17,7 +19,7 @@ class Person(models.Model):
|
||||
super(Person, self).__init__(*args, **kwargs)
|
||||
self.data = []
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s %s" % (self.first_name, self.last_name)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
|
||||
@@ -8,52 +8,61 @@ the select-related behavior will traverse.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
# Who remembers high school biology?
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Domain(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Kingdom(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
domain = models.ForeignKey(Domain)
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Phylum(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
kingdom = models.ForeignKey(Kingdom)
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Klass(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
phylum = models.ForeignKey(Phylum)
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Order(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
klass = models.ForeignKey(Klass)
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Family(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
order = models.ForeignKey(Order)
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Genus(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
family = models.ForeignKey(Family)
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Species(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
genus = models.ForeignKey(Genus)
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@@ -11,28 +11,32 @@ from decimal import Decimal
|
||||
|
||||
from django.db import models
|
||||
from django.utils import six
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Category(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Author(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Article(models.Model):
|
||||
author = models.ForeignKey(Author)
|
||||
headline = models.CharField(max_length=50)
|
||||
@@ -42,28 +46,31 @@ class Article(models.Model):
|
||||
class Meta:
|
||||
ordering = ('pub_date',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class AuthorProfile(models.Model):
|
||||
author = models.OneToOneField(Author, primary_key=True)
|
||||
date_of_birth = models.DateField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "Profile of %s" % self.author
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Actor(models.Model):
|
||||
name = models.CharField(max_length=20, primary_key=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ('name',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Movie(models.Model):
|
||||
actor = models.ForeignKey(Actor)
|
||||
title = models.CharField(max_length=50)
|
||||
@@ -72,7 +79,7 @@ class Movie(models.Model):
|
||||
class Meta:
|
||||
ordering = ('title',)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
|
||||
@@ -80,13 +87,11 @@ class Score(models.Model):
|
||||
score = models.FloatField()
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Team(object):
|
||||
def __init__(self, title):
|
||||
self.title = title
|
||||
|
||||
def __unicode__(self):
|
||||
raise NotImplementedError("Not so simple")
|
||||
|
||||
def __str__(self):
|
||||
raise NotImplementedError("Not so simple")
|
||||
|
||||
@@ -94,8 +99,7 @@ class Team(object):
|
||||
return "%s" % self.title
|
||||
|
||||
|
||||
class TeamField(models.CharField):
|
||||
__metaclass__ = models.SubfieldBase
|
||||
class TeamField(six.with_metaclass(models.SubfieldBase, models.CharField)):
|
||||
|
||||
def __init__(self):
|
||||
super(TeamField, self).__init__(max_length=100)
|
||||
@@ -112,10 +116,11 @@ class TeamField(models.CharField):
|
||||
return self._get_val_from_obj(obj).to_string()
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Player(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
rank = models.IntegerField()
|
||||
team = TeamField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return '%s (%d) playing for %s' % (self.name, self.rank, self.team.to_string())
|
||||
|
||||
@@ -114,8 +114,8 @@ class SerializersTestBase(object):
|
||||
Tests the ability to create new objects by
|
||||
modifying serialized content.
|
||||
"""
|
||||
old_headline = b"Poker has no place on ESPN"
|
||||
new_headline = b"Poker has no place on television"
|
||||
old_headline = "Poker has no place on ESPN"
|
||||
new_headline = "Poker has no place on television"
|
||||
serial_str = serializers.serialize(self.serializer_name,
|
||||
Article.objects.all())
|
||||
serial_str = serial_str.replace(old_headline, new_headline)
|
||||
@@ -285,7 +285,7 @@ class SerializersTransactionTestBase(object):
|
||||
|
||||
class XmlSerializerTestCase(SerializersTestBase, TestCase):
|
||||
serializer_name = "xml"
|
||||
pkless_str = b"""<?xml version="1.0" encoding="utf-8"?>
|
||||
pkless_str = """<?xml version="1.0" encoding="utf-8"?>
|
||||
<django-objects version="1.0">
|
||||
<object model="serializers.category">
|
||||
<field type="CharField" name="name">Reference</field>
|
||||
@@ -331,7 +331,7 @@ class XmlSerializerTestCase(SerializersTestBase, TestCase):
|
||||
|
||||
class XmlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
|
||||
serializer_name = "xml"
|
||||
fwd_ref_str = b"""<?xml version="1.0" encoding="utf-8"?>
|
||||
fwd_ref_str = """<?xml version="1.0" encoding="utf-8"?>
|
||||
<django-objects version="1.0">
|
||||
<object pk="1" model="serializers.article">
|
||||
<field to="serializers.author" name="author" rel="ManyToOneRel">1</field>
|
||||
@@ -351,7 +351,7 @@ class XmlSerializerTransactionTestCase(SerializersTransactionTestBase, Transacti
|
||||
|
||||
class JsonSerializerTestCase(SerializersTestBase, TestCase):
|
||||
serializer_name = "json"
|
||||
pkless_str = b"""[{"pk": null, "model": "serializers.category", "fields": {"name": "Reference"}}]"""
|
||||
pkless_str = """[{"pk": null, "model": "serializers.category", "fields": {"name": "Reference"}}]"""
|
||||
|
||||
@staticmethod
|
||||
def _validate_output(serial_str):
|
||||
@@ -381,7 +381,7 @@ class JsonSerializerTestCase(SerializersTestBase, TestCase):
|
||||
|
||||
class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
|
||||
serializer_name = "json"
|
||||
fwd_ref_str = b"""[
|
||||
fwd_ref_str = """[
|
||||
{
|
||||
"pk": 1,
|
||||
"model": "serializers.article",
|
||||
@@ -414,7 +414,7 @@ except ImportError:
|
||||
else:
|
||||
class YamlSerializerTestCase(SerializersTestBase, TestCase):
|
||||
serializer_name = "yaml"
|
||||
fwd_ref_str = b"""- fields:
|
||||
fwd_ref_str = """- fields:
|
||||
headline: Forward references pose no problem
|
||||
pub_date: 2006-06-16 15:00:00
|
||||
categories: [1]
|
||||
@@ -430,7 +430,7 @@ else:
|
||||
pk: 1
|
||||
model: serializers.author"""
|
||||
|
||||
pkless_str = b"""- fields:
|
||||
pkless_str = """- fields:
|
||||
name: Reference
|
||||
pk: null
|
||||
model: serializers.category"""
|
||||
@@ -470,7 +470,7 @@ else:
|
||||
|
||||
class YamlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
|
||||
serializer_name = "yaml"
|
||||
fwd_ref_str = b"""- fields:
|
||||
fwd_ref_str = """- fields:
|
||||
headline: Forward references pose no problem
|
||||
pub_date: 2006-06-16 15:00:00
|
||||
categories: [1]
|
||||
|
||||
@@ -4,18 +4,21 @@ Testing signals before/after saving and deleting.
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
first_name = models.CharField(max_length=20)
|
||||
last_name = models.CharField(max_length=20)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s %s" % (self.first_name, self.last_name)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Car(models.Model):
|
||||
make = models.CharField(max_length=20)
|
||||
model = models.CharField(max_length=20)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s %s" % (self.make, self.model)
|
||||
|
||||
@@ -15,6 +15,7 @@ if you prefer. You must be careful to encode the results correctly, though.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
class Article(models.Model):
|
||||
@@ -26,9 +27,10 @@ class Article(models.Model):
|
||||
# in ASCII.
|
||||
return self.headline
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class InternationalArticle(models.Model):
|
||||
headline = models.CharField(max_length=100)
|
||||
pub_date = models.DateTimeField()
|
||||
|
||||
def __unicode__(self):
|
||||
return self.headline
|
||||
def __str__(self):
|
||||
return self.headline
|
||||
|
||||
@@ -4,23 +4,32 @@ from __future__ import absolute_import, unicode_literals
|
||||
import datetime
|
||||
|
||||
from django.test import TestCase
|
||||
from django.utils import six
|
||||
from django.utils.unittest import skipIf
|
||||
|
||||
from .models import Article, InternationalArticle
|
||||
|
||||
|
||||
class SimpleTests(TestCase):
|
||||
|
||||
@skipIf(six.PY3, "tests a __str__ method returning unicode under Python 2")
|
||||
def test_basic(self):
|
||||
a = Article.objects.create(
|
||||
headline=b'Area man programs in Python',
|
||||
pub_date=datetime.datetime(2005, 7, 28)
|
||||
)
|
||||
self.assertEqual(str(a), b'Area man programs in Python')
|
||||
self.assertEqual(repr(a), b'<Article: Area man programs in Python>')
|
||||
self.assertEqual(str(a), str('Area man programs in Python'))
|
||||
self.assertEqual(repr(a), str('<Article: Area man programs in Python>'))
|
||||
|
||||
def test_international(self):
|
||||
a = InternationalArticle.objects.create(
|
||||
headline='Girl wins €12.500 in lottery',
|
||||
pub_date=datetime.datetime(2005, 7, 28)
|
||||
)
|
||||
# The default str() output will be the UTF-8 encoded output of __unicode__().
|
||||
self.assertEqual(str(a), b'Girl wins \xe2\x82\xac12.500 in lottery')
|
||||
if six.PY3:
|
||||
self.assertEqual(str(a), 'Girl wins €12.500 in lottery')
|
||||
else:
|
||||
# On Python 2, the default str() output will be the UTF-8 encoded
|
||||
# output of __unicode__() -- or __str__() when the
|
||||
# python_2_unicode_compatible decorator is used.
|
||||
self.assertEqual(str(a), b'Girl wins \xe2\x82\xac12.500 in lottery')
|
||||
|
||||
@@ -91,7 +91,7 @@ class ClientTest(TestCase):
|
||||
content_type="text/xml")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.templates[0].name, "Book template")
|
||||
self.assertEqual(response.content, "Blink - Malcolm Gladwell")
|
||||
self.assertEqual(response.content, b"Blink - Malcolm Gladwell")
|
||||
|
||||
def test_redirect(self):
|
||||
"GET a URL that redirects elsewhere"
|
||||
|
||||
@@ -467,6 +467,7 @@ class NewDatabaseTests(TestCase):
|
||||
[event],
|
||||
transform=lambda d: d)
|
||||
|
||||
@requires_tz_support
|
||||
def test_filter_date_field_with_aware_datetime(self):
|
||||
# Regression test for #17742
|
||||
day = datetime.date(2011, 9, 1)
|
||||
|
||||
@@ -9,8 +9,10 @@ manually.
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Reporter(models.Model):
|
||||
first_name = models.CharField(max_length=30)
|
||||
last_name = models.CharField(max_length=30)
|
||||
@@ -19,5 +21,5 @@ class Reporter(models.Model):
|
||||
class Meta:
|
||||
ordering = ('first_name', 'last_name')
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return "%s %s" % (self.first_name, self.last_name)
|
||||
|
||||
@@ -4,9 +4,11 @@ is generated for the table on various manage.py operations.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
# All of these models are created in the database by Django.
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class A01(models.Model):
|
||||
f_a = models.CharField(max_length=10, db_index=True)
|
||||
f_b = models.IntegerField()
|
||||
@@ -14,9 +16,10 @@ class A01(models.Model):
|
||||
class Meta:
|
||||
db_table = 'a01'
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.f_a
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class B01(models.Model):
|
||||
fk_a = models.ForeignKey(A01)
|
||||
f_a = models.CharField(max_length=10, db_index=True)
|
||||
@@ -27,9 +30,10 @@ class B01(models.Model):
|
||||
# 'managed' is True by default. This tests we can set it explicitly.
|
||||
managed = True
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.f_a
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class C01(models.Model):
|
||||
mm_a = models.ManyToManyField(A01, db_table='d01')
|
||||
f_a = models.CharField(max_length=10, db_index=True)
|
||||
@@ -38,13 +42,14 @@ class C01(models.Model):
|
||||
class Meta:
|
||||
db_table = 'c01'
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.f_a
|
||||
|
||||
# All of these models use the same tables as the previous set (they are shadows
|
||||
# of possibly a subset of the columns). There should be no creation errors,
|
||||
# since we have told Django they aren't managed by Django.
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class A02(models.Model):
|
||||
f_a = models.CharField(max_length=10, db_index=True)
|
||||
|
||||
@@ -52,9 +57,10 @@ class A02(models.Model):
|
||||
db_table = 'a01'
|
||||
managed = False
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.f_a
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class B02(models.Model):
|
||||
class Meta:
|
||||
db_table = 'b01'
|
||||
@@ -64,11 +70,12 @@ class B02(models.Model):
|
||||
f_a = models.CharField(max_length=10, db_index=True)
|
||||
f_b = models.IntegerField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.f_a
|
||||
|
||||
# To re-use the many-to-many intermediate table, we need to manually set up
|
||||
# things up.
|
||||
@python_2_unicode_compatible
|
||||
class C02(models.Model):
|
||||
mm_a = models.ManyToManyField(A02, through="Intermediate")
|
||||
f_a = models.CharField(max_length=10, db_index=True)
|
||||
@@ -78,7 +85,7 @@ class C02(models.Model):
|
||||
db_table = 'c01'
|
||||
managed = False
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.f_a
|
||||
|
||||
class Intermediate(models.Model):
|
||||
|
||||
@@ -5,21 +5,24 @@ updates.
|
||||
|
||||
from django.db import models
|
||||
from django.utils import six
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class DataPoint(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
value = models.CharField(max_length=20)
|
||||
another_value = models.CharField(max_length=20, blank=True)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return six.text_type(self.name)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class RelatedPoint(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
data = models.ForeignKey(DataPoint)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return six.text_type(self.name)
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
GENDER_CHOICES = (
|
||||
('M', 'Male'),
|
||||
@@ -10,11 +11,13 @@ class Account(models.Model):
|
||||
num = models.IntegerField()
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
|
||||
pid = models.IntegerField(null=True, default=None)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
@@ -24,11 +27,12 @@ class Employee(Person):
|
||||
accounts = models.ManyToManyField('Account', related_name='employees', blank=True, null=True)
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Profile(models.Model):
|
||||
name = models.CharField(max_length=200)
|
||||
salary = models.FloatField(default=1000.0)
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
|
||||
@@ -18,6 +18,107 @@ class UpdateOnlyFieldsTests(TestCase):
|
||||
self.assertEqual(s.gender, 'F')
|
||||
self.assertEqual(s.name, 'Ian')
|
||||
|
||||
def test_update_fields_deferred(self):
|
||||
s = Person.objects.create(name='Sara', gender='F', pid=22)
|
||||
self.assertEqual(s.gender, 'F')
|
||||
|
||||
s1 = Person.objects.defer("gender", "pid").get(pk=s.pk)
|
||||
s1.name = "Emily"
|
||||
s1.gender = "M"
|
||||
|
||||
with self.assertNumQueries(1):
|
||||
s1.save()
|
||||
|
||||
s2 = Person.objects.get(pk=s1.pk)
|
||||
self.assertEqual(s2.name, "Emily")
|
||||
self.assertEqual(s2.gender, "M")
|
||||
|
||||
def test_update_fields_only_1(self):
|
||||
s = Person.objects.create(name='Sara', gender='F')
|
||||
self.assertEqual(s.gender, 'F')
|
||||
|
||||
s1 = Person.objects.only('name').get(pk=s.pk)
|
||||
s1.name = "Emily"
|
||||
s1.gender = "M"
|
||||
|
||||
with self.assertNumQueries(1):
|
||||
s1.save()
|
||||
|
||||
s2 = Person.objects.get(pk=s1.pk)
|
||||
self.assertEqual(s2.name, "Emily")
|
||||
self.assertEqual(s2.gender, "M")
|
||||
|
||||
def test_update_fields_only_2(self):
|
||||
s = Person.objects.create(name='Sara', gender='F', pid=22)
|
||||
self.assertEqual(s.gender, 'F')
|
||||
|
||||
s1 = Person.objects.only('name').get(pk=s.pk)
|
||||
s1.name = "Emily"
|
||||
s1.gender = "M"
|
||||
|
||||
with self.assertNumQueries(2):
|
||||
s1.save(update_fields=['pid'])
|
||||
|
||||
s2 = Person.objects.get(pk=s1.pk)
|
||||
self.assertEqual(s2.name, "Sara")
|
||||
self.assertEqual(s2.gender, "F")
|
||||
|
||||
def test_update_fields_only_repeated(self):
|
||||
s = Person.objects.create(name='Sara', gender='F')
|
||||
self.assertEqual(s.gender, 'F')
|
||||
|
||||
s1 = Person.objects.only('name').get(pk=s.pk)
|
||||
s1.gender = 'M'
|
||||
with self.assertNumQueries(1):
|
||||
s1.save()
|
||||
# Test that the deferred class does not remember that gender was
|
||||
# set, instead the instace should remember this.
|
||||
s1 = Person.objects.only('name').get(pk=s.pk)
|
||||
with self.assertNumQueries(1):
|
||||
s1.save()
|
||||
|
||||
def test_update_fields_inheritance_defer(self):
|
||||
profile_boss = Profile.objects.create(name='Boss', salary=3000)
|
||||
e1 = Employee.objects.create(name='Sara', gender='F',
|
||||
employee_num=1, profile=profile_boss)
|
||||
e1 = Employee.objects.only('name').get(pk=e1.pk)
|
||||
e1.name = 'Linda'
|
||||
with self.assertNumQueries(1):
|
||||
e1.save()
|
||||
self.assertEqual(Employee.objects.get(pk=e1.pk).name,
|
||||
'Linda')
|
||||
|
||||
def test_update_fields_fk_defer(self):
|
||||
profile_boss = Profile.objects.create(name='Boss', salary=3000)
|
||||
profile_receptionist = Profile.objects.create(name='Receptionist', salary=1000)
|
||||
e1 = Employee.objects.create(name='Sara', gender='F',
|
||||
employee_num=1, profile=profile_boss)
|
||||
e1 = Employee.objects.only('profile').get(pk=e1.pk)
|
||||
e1.profile = profile_receptionist
|
||||
with self.assertNumQueries(1):
|
||||
e1.save()
|
||||
self.assertEqual(Employee.objects.get(pk=e1.pk).profile, profile_receptionist)
|
||||
e1.profile_id = profile_boss.pk
|
||||
with self.assertNumQueries(1):
|
||||
e1.save()
|
||||
self.assertEqual(Employee.objects.get(pk=e1.pk).profile, profile_boss)
|
||||
|
||||
def test_select_related_only_interaction(self):
|
||||
profile_boss = Profile.objects.create(name='Boss', salary=3000)
|
||||
e1 = Employee.objects.create(name='Sara', gender='F',
|
||||
employee_num=1, profile=profile_boss)
|
||||
e1 = Employee.objects.only('profile__salary').select_related('profile').get(pk=e1.pk)
|
||||
profile_boss.name = 'Clerk'
|
||||
profile_boss.salary = 1000
|
||||
profile_boss.save()
|
||||
# The loaded salary of 3000 gets saved, the name of 'Clerk' isn't
|
||||
# overwritten.
|
||||
with self.assertNumQueries(1):
|
||||
e1.profile.save()
|
||||
reloaded_profile = Profile.objects.get(pk=profile_boss.pk)
|
||||
self.assertEqual(reloaded_profile.name, profile_boss.name)
|
||||
self.assertEqual(reloaded_profile.salary, 3000)
|
||||
|
||||
def test_update_fields_m2m(self):
|
||||
profile_boss = Profile.objects.create(name='Boss', salary=3000)
|
||||
e1 = Employee.objects.create(name='Sara', gender='F',
|
||||
|
||||
@@ -4,6 +4,7 @@ from datetime import datetime
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
|
||||
def validate_answer_to_universe(value):
|
||||
@@ -66,13 +67,14 @@ class Article(models.Model):
|
||||
if self.pub_date is None:
|
||||
self.pub_date = datetime.now()
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Post(models.Model):
|
||||
title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
|
||||
slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
|
||||
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
|
||||
posted = models.DateField()
|
||||
|
||||
def __unicode__(self):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class FlexibleDatePost(models.Model):
|
||||
@@ -89,6 +91,8 @@ class GenericIPAddressTestModel(models.Model):
|
||||
generic_ip = models.GenericIPAddressField(blank=True, null=True, unique=True)
|
||||
v4_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv4")
|
||||
v6_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv6")
|
||||
ip_verbose_name = models.GenericIPAddressField("IP Address Verbose",
|
||||
blank=True, null=True)
|
||||
|
||||
class GenericIPAddrUnpackUniqueTest(models.Model):
|
||||
generic_v4unpack_ip = models.GenericIPAddressField(blank=True, unique=True, unpack_ipv4=True)
|
||||
|
||||
@@ -79,7 +79,7 @@ class ModelFormsTests(TestCase):
|
||||
'pub_date': '2010-1-10 14:49:00'
|
||||
}
|
||||
form = ArticleForm(data)
|
||||
self.assertEqual(form.errors.keys(), [])
|
||||
self.assertEqual(list(form.errors), [])
|
||||
article = form.save(commit=False)
|
||||
article.author = self.author
|
||||
article.save()
|
||||
@@ -95,7 +95,7 @@ class ModelFormsTests(TestCase):
|
||||
}
|
||||
article = Article(author_id=self.author.id)
|
||||
form = ArticleForm(data, instance=article)
|
||||
self.assertEqual(form.errors.keys(), [])
|
||||
self.assertEqual(list(form.errors), [])
|
||||
self.assertNotEqual(form.instance.pub_date, None)
|
||||
article = form.save()
|
||||
|
||||
@@ -108,7 +108,7 @@ class ModelFormsTests(TestCase):
|
||||
}
|
||||
article = Article(author_id=self.author.id)
|
||||
form = ArticleForm(data, instance=article)
|
||||
self.assertEqual(form.errors.keys(), ['pub_date'])
|
||||
self.assertEqual(list(form.errors), ['pub_date'])
|
||||
|
||||
|
||||
class GenericIPAddressFieldTests(ValidationTestCase):
|
||||
|
||||
Reference in New Issue
Block a user