From 3b0e5d8d2950803c5503dae3af7565987a36f983 Mon Sep 17 00:00:00 2001 From: Joseph Kocherhans Date: Mon, 1 May 2006 15:02:39 +0000 Subject: [PATCH] magic-removal: Fixed #1681. OneToOneField now works properly in the admin system. git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2800 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../admin/templates/admin/field_line.html | 8 ----- .../admin/templates/widget/foreign.html | 8 +++++ .../admin/templates/widget/one_to_one.html | 3 +- django/db/models/fields/related.py | 34 +++++++++---------- docs/model-api.txt | 12 ++----- 5 files changed, 29 insertions(+), 36 deletions(-) diff --git a/django/contrib/admin/templates/admin/field_line.html b/django/contrib/admin/templates/admin/field_line.html index 10f37d5dc9..b7e2fc2ae0 100644 --- a/django/contrib/admin/templates/admin/field_line.html +++ b/django/contrib/admin/templates/admin/field_line.html @@ -9,14 +9,6 @@ {% if not bound_field.has_label_first %} {% field_label bound_field %} {% endif %} - {% if change %} - {% if bound_field.field.primary_key %} - {{ bound_field.original_value }} - {% endif %} - {% if bound_field.raw_id_admin %} - {% if bound_field.existing_display %} {{ bound_field.existing_display|truncatewords:"14" }}{% endif %} - {% endif %} - {% endif %} {% if bound_field.field.help_text %}

{{ bound_field.field.help_text }}

{% endif %} {% endfor %} diff --git a/django/contrib/admin/templates/widget/foreign.html b/django/contrib/admin/templates/widget/foreign.html index b8e825bdae..6b43d044bd 100644 --- a/django/contrib/admin/templates/widget/foreign.html +++ b/django/contrib/admin/templates/widget/foreign.html @@ -10,3 +10,11 @@ {% if bound_field.needs_add_label %} Add Another {% endif %}{% endif %} +{% if change %} + {% if bound_field.field.primary_key %} + {{ bound_field.original_value }} + {% endif %} + {% if bound_field.raw_id_admin %} + {% if bound_field.existing_display %} {{ bound_field.existing_display|truncatewords:"14" }}{% endif %} + {% endif %} +{% endif %} diff --git a/django/contrib/admin/templates/widget/one_to_one.html b/django/contrib/admin/templates/widget/one_to_one.html index a93aa65f73..a79a12314f 100644 --- a/django/contrib/admin/templates/widget/one_to_one.html +++ b/django/contrib/admin/templates/widget/one_to_one.html @@ -1 +1,2 @@ -{% include "widget/foreign.html" %} +{% if add %}{% include "widget/foreign.html" %}{% endif %} +{% if change %}{% if bound_field.existing_display %} {{ bound_field.existing_display|truncatewords:"14" }}{% endif %}{% endif %} diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 692c02b48a..2e61458800 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -76,6 +76,23 @@ class RelatedField(object): # but this can be overridden with the "related_name" option. return self.rel.related_name or opts.object_name.lower() + def prepare_field_objs_and_params(self, manipulator, name_prefix): + params = {'validator_list': self.validator_list[:], 'member_name': name_prefix + self.attname} + if self.rel.raw_id_admin: + field_objs = self.get_manipulator_field_objs() + params['validator_list'].append(curry(manipulator_valid_rel_key, self, manipulator)) + else: + if self.radio_admin: + field_objs = [forms.RadioSelectField] + params['ul_class'] = get_ul_class(self.radio_admin) + else: + if self.null: + field_objs = [forms.NullSelectField] + else: + field_objs = [forms.SelectField] + params['choices'] = self.get_choices_default() + return field_objs, params + class SingleRelatedObjectDescriptor(object): # This class provides the functionality that makes the related-object # managers available as attributes on a model class, for fields that have @@ -451,23 +468,6 @@ class ForeignKey(RelatedField, Field): def get_validator_unique_lookup_type(self): return '%s__%s__exact' % (self.name, self.rel.get_related_field().name) - def prepare_field_objs_and_params(self, manipulator, name_prefix): - params = {'validator_list': self.validator_list[:], 'member_name': name_prefix + self.attname} - if self.rel.raw_id_admin: - field_objs = self.get_manipulator_field_objs() - params['validator_list'].append(curry(manipulator_valid_rel_key, self, manipulator)) - else: - if self.radio_admin: - field_objs = [forms.RadioSelectField] - params['ul_class'] = get_ul_class(self.radio_admin) - else: - if self.null: - field_objs = [forms.NullSelectField] - else: - field_objs = [forms.SelectField] - params['choices'] = self.get_choices_default() - return field_objs, params - def get_manipulator_field_objs(self): rel_field = self.rel.get_related_field() if self.rel.raw_id_admin and not isinstance(rel_field, AutoField): diff --git a/docs/model-api.txt b/docs/model-api.txt index 1c65b8614c..44253f301b 100644 --- a/docs/model-api.txt +++ b/docs/model-api.txt @@ -877,16 +877,8 @@ string ``"self"`` instead of the model name; references to as-yet undefined models can be made by using a string containing the model name. This ``OneToOneField`` will actually replace the primary key ``id`` field -(since one-to-one relations share the same primary key), and has a few -differences in the admin interface: - - * No ``Place`` selection interface is displayed on ``Restaurant`` pages. - There will be one (and only one) ``Restaurant`` for each ``Place``. - - * On the ``Restaurant`` change list, every ``Place`` -- whether it has an - associated ``Restaurant`` or not -- will be displayed. Adding a - ``Restaurant`` to a ``Place`` just means filling out the required - ``Restaurant`` fields. +(since one-to-one relations share the same primary key), and will be displayed +as a read-only field when you edit an object in the admin interface: See the `One-to-one relationship model example`_ for a full example.