Author: mtredinnick
Date: 2008-12-08 02:23:52 -0600 (Mon, 08 Dec 2008)
New Revision: 9603

Modified:
   django/branches/releases/1.0.X/django/db/models/base.py
   django/branches/releases/1.0.X/django/db/models/options.py
   django/branches/releases/1.0.X/django/forms/models.py
Log:
[1.0.X] The first step in fixing a group of problems related to outputting a
proper "value" for a field that is a relation to another model.

This part adds the utility method on Model that should help in general.Also
cleans up the slightly ugly mess from r8957.

Backport of r9601 from trunk (the second piece of this patch is a bugfix, not
just a tidy-up. It looks like it might be possible to have to_field setups that
make the existing code fail and that's only hidden by the fact that inherited
models with to_field relations to the parent fail for other reasons right now).


Modified: django/branches/releases/1.0.X/django/db/models/base.py
===================================================================
--- django/branches/releases/1.0.X/django/db/models/base.py     2008-12-08 
08:16:26 UTC (rev 9602)
+++ django/branches/releases/1.0.X/django/db/models/base.py     2008-12-08 
08:23:52 UTC (rev 9603)
@@ -296,6 +296,16 @@
 
     pk = property(_get_pk_val, _set_pk_val)
 
+    def serializable_value(self, field_name):
+        """
+        Returns the value of the field name for this instance. If the field
+        is a foreign key, returns the id value, instead of the object.
+        Used to serialize a field's value (in the serializer, or form output,
+        for example).
+        """
+        field = self._meta.get_field_by_name(field_name)[0]
+        return getattr(self, field.attname)
+
     def save(self, force_insert=False, force_update=False):
         """
         Saves the current instance. Override this in a subclass if you want to

Modified: django/branches/releases/1.0.X/django/db/models/options.py
===================================================================
--- django/branches/releases/1.0.X/django/db/models/options.py  2008-12-08 
08:16:26 UTC (rev 9602)
+++ django/branches/releases/1.0.X/django/db/models/options.py  2008-12-08 
08:23:52 UTC (rev 9603)
@@ -448,3 +448,4 @@
             #        objects.append(opts)
             self._ordered_objects = objects
         return self._ordered_objects
+

Modified: django/branches/releases/1.0.X/django/forms/models.py
===================================================================
--- django/branches/releases/1.0.X/django/forms/models.py       2008-12-08 
08:16:26 UTC (rev 9602)
+++ django/branches/releases/1.0.X/django/forms/models.py       2008-12-08 
08:23:52 UTC (rev 9603)
@@ -624,13 +624,7 @@
 
     def choice(self, obj):
         if self.field.to_field_name:
-            # FIXME: The try..except shouldn't be necessary here. But this is
-            # going in just before 1.0, so I want to be careful. Will check it
-            # out later.
-            try:
-                key = getattr(obj, self.field.to_field_name).pk
-            except AttributeError:
-                key = getattr(obj, self.field.to_field_name)
+            key = obj.serializable_value(self.field.to_field_name)
         else:
             key = obj.pk
         return (key, self.field.label_from_instance(obj))


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to