Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@adrianholovaty adrianholovaty authored
Showing with 19 additions and 3 deletions.
  1. +17 −0 django/core/meta.py
  2. +2 −3 docs/db-api.txt
View
17 django/core/meta.py
@@ -2076,6 +2076,23 @@ def get_manipulator_field_objs(self):
choices = self.get_choices(include_blank=False)
return [curry(formfields.SelectMultipleField, size=min(max(len(choices), 5), 15), choices=choices)]
+class OneToOneField(IntegerField):
+ def __init__(self, to, to_field=None, rel_name=None, **kwargs):
+ kwargs['name'] = kwargs.get('name', 'id')
+ kwargs['verbose_name'] = kwargs.get('verbose_name', 'ID')
+ to_field = to_field or to._meta.pk.name
+ rel_name = rel_name or to._meta.object_name.lower()
+ kwargs['rel'] = OneToOne(to, rel_name, to_field,
+ num_in_admin=kwargs.pop('num_in_admin', 0),
+ edit_inline=kwargs.pop('edit_inline', False),
+ edit_inline_type=kwargs.pop('edit_inline_type', STACKED),
+ related_name=kwargs.pop('related_name', None),
+ limit_choices_to=kwargs.pop('limit_choices_to', None),
+ lookup_overrides=kwargs.pop('lookup_overrides', None),
+ raw_id_admin=kwargs.pop('raw_id_admin', False))
+ kwargs['primary_key'] = True
+ IntegerField.__init__(self, **kwargs)
+
####################
# RELATIONSHIPS #
####################
View
5 docs/db-api.txt
@@ -141,8 +141,7 @@ method. For example::
class Restaurant(meta.Model):
...
fields = (
- meta.IntegerField('id', 'ID', primary_key=True,
- rel=meta.OneToOne(places.Place, 'place', 'id')),
+ meta.OneToOneField(places.Place),
...
)
@@ -294,7 +293,7 @@ For example::
Changing objects
================
-Once you've retrieved an object from the database using any of the above
+Once you've retrieved an object from the database using any of the above
options, changing it is extremely easy. Make changes directly to the
objects fields, then call the object's ``save()`` method::
Please sign in to comment.
Something went wrong with that request. Please try again.