Ticket #10785: custom_pk.2.diff
File custom_pk.2.diff, 5.3 KB (added by , 16 years ago) |
---|
-
django/db/models/fields/related.py
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 1d083cd..586d80c 100644
a b class RelatedField(object): 132 132 v, field = getattr(v, v._meta.pk.name), v._meta.pk 133 133 except AttributeError: 134 134 pass 135 if field: 136 if lookup_type in ('range', 'in'): 137 v = [v] 138 v = field.get_db_prep_lookup(lookup_type, v) 139 if isinstance(v, list): 140 v = v[0] 135 if not field: 136 field = self.rel.get_related_field() 137 if lookup_type in ('range', 'in'): 138 v = [v] 139 v = field.get_db_prep_lookup(lookup_type, v) 140 if isinstance(v, list): 141 v = v[0] 141 142 return v 142 143 143 144 if hasattr(value, 'as_sql') or hasattr(value, '_as_sql'): … … class ManyToManyField(RelatedField, Field): 955 956 # A ManyToManyField is not represented by a single column, 956 957 # so return None. 957 958 return None 958 -
django/db/models/sql/where.py
diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py index ec0545c..0379aed 100644
a b class Constraint(object): 278 278 raise EmptyShortCircuit 279 279 280 280 return (self.alias, self.col, db_type), params 281 -
new file tests/modeltests/custom_pk/fields.py
diff --git a/tests/modeltests/custom_pk/fields.py b/tests/modeltests/custom_pk/fields.py new file mode 100644 index 0000000..319e42f
- + 1 import random 2 import string 3 4 from django.db import models 5 6 class MyWrapper(object): 7 def __init__(self, value): 8 self.value = value 9 10 def __repr__(self): 11 return "<%s: %s>" % (self.__class__.__name__, self.value) 12 13 def __unicode__(self): 14 return self.value 15 16 def __eq__(self, other): 17 if isinstance(other, self.__class__): 18 return self.value == other.value 19 return self.value == other 20 21 class MyAutoField(models.CharField): 22 __metaclass__ = models.SubfieldBase 23 24 def __init__(self, *args, **kwargs): 25 kwargs['max_length'] = 10 26 super(MyAutoField, self).__init__(*args, **kwargs) 27 28 def pre_save(self, instance, add): 29 value = getattr(instance, self.attname, None) 30 if not value: 31 value = MyWrapper(''.join(random.sample(string.lowercase, 10))) 32 setattr(instance, self.attname, value) 33 return value 34 35 def to_python(self, value): 36 if not value: 37 return 38 if not isinstance(value, MyWrapper): 39 value = MyWrapper(value) 40 return value 41 42 def get_db_prep_save(self, value): 43 if not value: 44 return 45 if isinstance(value, MyWrapper): 46 return unicode(value) 47 return value 48 49 def get_db_prep_value(self, value): 50 if not value: 51 return 52 if isinstance(value, MyWrapper): 53 return unicode(value) 54 return value -
tests/modeltests/custom_pk/models.py
diff --git a/tests/modeltests/custom_pk/models.py b/tests/modeltests/custom_pk/models.py index 091f7f3..0323fec 100644
a b this behavior by explicitly adding ``primary_key=True`` to a field. 9 9 from django.conf import settings 10 10 from django.db import models, transaction, IntegrityError 11 11 12 from fields import MyAutoField 13 12 14 class Employee(models.Model): 13 15 employee_code = models.IntegerField(primary_key=True, db_column = 'code') 14 16 first_name = models.CharField(max_length=20) … … class Business(models.Model): 28 30 def __unicode__(self): 29 31 return self.name 30 32 33 class Bar(models.Model): 34 id = MyAutoField(primary_key=True, db_index=True) 35 36 def __unicode__(self): 37 return repr(self.pk) 38 39 40 class Foo(models.Model): 41 bar = models.ForeignKey(Bar) 42 31 43 __test__ = {'API_TESTS':""" 32 44 >>> dan = Employee(employee_code=123, first_name='Dan', last_name='Jones') 33 45 >>> dan.save() … … DoesNotExist: Employee matching query does not exist. 121 133 ... print "Fail with %s" % type(e) 122 134 Pass 123 135 136 >>> new_bar = Bar.objects.create() 137 >>> new_foo = Foo.objects.create(bar=new_bar) 138 >>> f = Foo.objects.get(bar=new_bar.pk) 139 >>> f == new_foo 140 True 141 >>> f.bar == new_bar 142 True 124 143 """} 125 144 126 145 # SQLite lets objects be saved with an empty primary key, even though an -
tests/modeltests/model_forms/models.py
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 992bb90..295c557 100644
a b class ExplicitPK(models.Model): 189 189 def __unicode__(self): 190 190 return self.key 191 191 192 193 192 194 __test__ = {'API_TESTS': """ 193 195 >>> from django import forms 194 196 >>> from django.forms.models import ModelForm, model_to_dict … … ValidationError: [u'Select a valid choice. z is not one of the available choices 1472 1474 <tr><th><label for="id_description">Description:</label></th><td><input type="text" name="description" id="id_description" /></td></tr> 1473 1475 <tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr> 1474 1476 1477 1475 1478 # Clean up 1476 1479 >>> import shutil 1477 1480 >>> shutil.rmtree(temp_storage_dir)