Skip to content

Commit

Permalink
Fixed managing foreignkey in different backend engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Paro committed May 11, 2010
1 parent 62baf86 commit edcdc1d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README
Expand Up @@ -10,6 +10,12 @@ Future backends to support are:
- ...


IMPORTANT COMMITS:
------------------

- http://github.com/aparo/django/commit/62baf86ff872b3a271619a4327d7b08180c89337 allows to redefin the king of autofield in DatabaseOperations of an engine



All documentation is in the "docs" directory and online at
http://docs.djangoproject.com/en/dev/. If you're just getting started, here's
Expand Down
2 changes: 2 additions & 0 deletions django/__init__.py
@@ -1,5 +1,7 @@
VERSION = (1, 2, 0, 'rc', 1)

MODIFIED = True

def get_version():
version = '%s.%s' % (VERSION[0], VERSION[1])
if VERSION[2]:
Expand Down
9 changes: 7 additions & 2 deletions django/db/models/fields/related.py
Expand Up @@ -851,8 +851,13 @@ def get_db_prep_save(self, value, connection):
if value == '' or value == None:
return None
else:
return self.rel.get_related_field().get_db_prep_save(value,
connection=connection)
if connection.alias!=self.rel.to.objects.db:
#Cross database management
#TODO: check also engine name?
return value
else:
return self.rel.get_related_field().get_db_prep_save(value,
connection=connection)

def value_to_string(self, obj):
if not obj:
Expand Down
28 changes: 26 additions & 2 deletions django/forms/fields.py
Expand Up @@ -195,9 +195,13 @@ def to_python(self, value):
return smart_unicode(value)

def widget_attrs(self, widget):
attrs = {}
if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)):
# The HTML attribute is maxlength, not max_length.
return {'maxlength': str(self.max_length)}
attrs['maxlength'] = str(self.max_length)
if self.required and isinstance(widget, (TextInput, PasswordInput)):
attrs['required'] = "required"
return attrs

class IntegerField(Field):
default_error_messages = {
Expand All @@ -207,8 +211,9 @@ class IntegerField(Field):
}

def __init__(self, max_value=None, min_value=None, *args, **kwargs):
self.max_value = max_value
self.min_value = min_value
super(IntegerField, self).__init__(*args, **kwargs)

if max_value is not None:
self.validators.append(validators.MaxValueValidator(max_value))
if min_value is not None:
Expand All @@ -230,6 +235,16 @@ def to_python(self, value):
raise ValidationError(self.error_messages['invalid'])
return value

def widget_attrs(self, widget):
attrs = {}
if self.max_value is not None and isinstance(widget, TextInput):
attrs['max'] = str(self.max_value)
if self.min_value is not None and isinstance(widget, TextInput):
attrs['min'] = str(self.min_value)
if self.required and isinstance(widget, (TextInput, PasswordInput)):
attrs['required'] = "required"
return attrs

class FloatField(IntegerField):
default_error_messages = {
'invalid': _(u'Enter a number.'),
Expand Down Expand Up @@ -423,6 +438,15 @@ def __init__(self, regex, max_length=None, min_length=None, error_message=None,
self.regex = regex
self.validators.append(validators.RegexValidator(regex=regex))

def widget_attrs(self, widget):
attrs = {}
if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)):
# The HTML attribute is maxlength, not max_length.
attrs['maxlength'] = str(self.max_length)
if self.required and isinstance(widget, (TextInput, PasswordInput)):
attrs['required'] = "required"
return attrs

class EmailField(CharField):
default_error_messages = {
'invalid': _(u'Enter a valid e-mail address.'),
Expand Down

0 comments on commit edcdc1d

Please sign in to comment.