Ticket #10785: custom_pk.diff

File custom_pk.diff, 3.1 KB (added by Alex Gaynor, 16 years ago)

Patch that solved the problem. 2 problems with it: 1) The tests are in the wrong place, they're there since that's where the issue was originally reported to me. 2) The tests require py2.5 since that's when the UUID module was added

  • 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):  
    132132                    v, field = getattr(v, v._meta.pk.name), v._meta.pk
    133133            except AttributeError:
    134134                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]
    141142            return v
    142143
    143144        if hasattr(value, 'as_sql') or hasattr(value, '_as_sql'):
    class ManyToManyField(RelatedField, Field):  
    955956        # A ManyToManyField is not represented by a single column,
    956957        # so return None.
    957958        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):  
    278278            raise EmptyShortCircuit
    279279
    280280        return (self.alias, self.col, db_type), params
    281 
  • tests/modeltests/model_forms/models.py

    diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
    index 992bb90..3982518 100644
    a b import tempfile  
    1313from django.db import models
    1414from django.core.files.storage import FileSystemStorage
    1515
     16from fields import UUIDField
     17
    1618# Python 2.3 doesn't have sorted()
    1719try:
    1820    sorted
    class ExplicitPK(models.Model):  
    189191    def __unicode__(self):
    190192        return self.key
    191193
     194class Bar(models.Model):
     195    uuid = UUIDField(primary_key=True, db_index=True, auto=True)
     196
     197
     198class Foo(models.Model):
     199    book = models.ForeignKey(Book)
     200    bar = models.ForeignKey(Bar)
     201
     202    class Meta:
     203        unique_together = (('book', 'bar'),)
     204
    192205__test__ = {'API_TESTS': """
    193206>>> from django import forms
    194207>>> from django.forms.models import ModelForm, model_to_dict
    ValidationError: [u'Select a valid choice. z is not one of the available choices  
    14721485<tr><th><label for="id_description">Description:</label></th><td><input type="text" name="description" id="id_description" /></td></tr>
    14731486<tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr>
    14741487
     1488>>> class FooForm(forms.ModelForm):
     1489...     class Meta:
     1490...         model = Foo
     1491
     1492>>> new_bar = Bar.objects.create()
     1493>>> data = {
     1494...     'bar': str(new_bar.uuid),
     1495...     'book': str(Book.objects.all()[0].id)
     1496... }
     1497>>> f = FooForm(data)
     1498>>> new_foo = f.save()
     1499>>> new_foo.bar == new_bar
     1500True
     1501
    14751502# Clean up
    14761503>>> import shutil
    14771504>>> shutil.rmtree(temp_storage_dir)
Back to Top