Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3 -- Made OneToOneField act like ForeignKey and ManyToManyFiel…

…d. This is the last big API change before the official launch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@59 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit dc2933f73b4a970d6b37241b6f185bbbacd1bb69 1 parent 7b47dcd
Adrian Holovaty authored July 15, 2005
17  django/core/meta.py
@@ -2076,6 +2076,23 @@ def get_manipulator_field_objs(self):
2076 2076
         choices = self.get_choices(include_blank=False)
2077 2077
         return [curry(formfields.SelectMultipleField, size=min(max(len(choices), 5), 15), choices=choices)]
2078 2078
 
  2079
+class OneToOneField(IntegerField):
  2080
+    def __init__(self, to, to_field=None, rel_name=None, **kwargs):
  2081
+        kwargs['name'] = kwargs.get('name', 'id')
  2082
+        kwargs['verbose_name'] = kwargs.get('verbose_name', 'ID')
  2083
+        to_field = to_field or to._meta.pk.name
  2084
+        rel_name = rel_name or to._meta.object_name.lower()
  2085
+        kwargs['rel'] = OneToOne(to, rel_name, to_field,
  2086
+            num_in_admin=kwargs.pop('num_in_admin', 0),
  2087
+            edit_inline=kwargs.pop('edit_inline', False),
  2088
+            edit_inline_type=kwargs.pop('edit_inline_type', STACKED),
  2089
+            related_name=kwargs.pop('related_name', None),
  2090
+            limit_choices_to=kwargs.pop('limit_choices_to', None),
  2091
+            lookup_overrides=kwargs.pop('lookup_overrides', None),
  2092
+            raw_id_admin=kwargs.pop('raw_id_admin', False))
  2093
+        kwargs['primary_key'] = True
  2094
+        IntegerField.__init__(self, **kwargs)
  2095
+
2079 2096
 ####################
2080 2097
 # RELATIONSHIPS    #
2081 2098
 ####################
5  docs/db-api.txt
@@ -141,8 +141,7 @@ method. For example::
141 141
     class Restaurant(meta.Model):
142 142
         ...
143 143
         fields = (
144  
-            meta.IntegerField('id', 'ID', primary_key=True,
145  
-                rel=meta.OneToOne(places.Place, 'place', 'id')),
  144
+            meta.OneToOneField(places.Place),
146 145
             ...
147 146
         )
148 147
 
@@ -294,7 +293,7 @@ For example::
294 293
 Changing objects
295 294
 ================
296 295
 
297  
-Once you've retrieved an object from the database using any of the above 
  296
+Once you've retrieved an object from the database using any of the above
298 297
 options, changing it is extremely easy.  Make changes directly to the
299 298
 objects fields, then call the object's ``save()`` method::
300 299
 

0 notes on commit dc2933f

Please sign in to comment.
Something went wrong with that request. Please try again.