Ticket #8241: onetoone_admin.8461.diff
File onetoone_admin.8461.diff, 2.7 KB (added by , 16 years ago) |
---|
-
django/forms/models.py
28 28 If commit=True, then the changes to ``instance`` will be saved to the 29 29 database. Returns ``instance``. 30 30 """ 31 from django.db import models31 from django.db.models import AutoField, OneToOneField 32 32 opts = instance._meta 33 33 if form.errors: 34 34 raise ValueError("The %s could not be %s because the data didn't" 35 35 " validate." % (opts.object_name, fail_message)) 36 36 cleaned_data = form.cleaned_data 37 37 for f in opts.fields: 38 if not f.editable or isinstance(f, models.AutoField) \38 if not f.editable or isinstance(f, (AutoField, OneToOneField)) \ 39 39 or not f.name in cleaned_data: 40 40 continue 41 41 if fields and f.name not in fields: … … 289 289 existing_objects[obj.pk] = obj 290 290 saved_instances = [] 291 291 for form in self.initial_forms: 292 obj = existing_objects[form.cleaned_data[self. model._meta.pk.attname]]292 obj = existing_objects[form.cleaned_data[self._pk_field_name]] 293 293 if self.can_delete and form.cleaned_data[DELETION_FIELD_NAME]: 294 294 self.deleted_objects.append(obj) 295 295 obj.delete() … … 317 317 318 318 def add_fields(self, form, index): 319 319 """Add a hidden field for the object's primary key.""" 320 if self.model._meta.has_auto_field: 321 self._pk_field_name = self.model._meta.pk.attname 320 from django.db.models import AutoField, OneToOneField 321 pk = self.model._meta.pk 322 while isinstance(pk, OneToOneField): 323 pk = pk.rel.to._meta.pk # Drill down until we find a "real" pk 324 if isinstance(pk, AutoField): 325 self._pk_field_name = self.model._meta.pk.name 322 326 form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput) 323 327 super(BaseModelFormSet, self).add_fields(form, index) 324 328 -
django/db/models/fields/related.py
744 744 """ 745 745 def __init__(self, to, to_field=None, **kwargs): 746 746 kwargs['unique'] = True 747 kwargs['editable'] = False 747 if not kwargs.get('primary_key', False): 748 kwargs['editable'] = False 748 749 if 'num_in_admin' not in kwargs: 749 750 kwargs['num_in_admin'] = 0 750 751 super(OneToOneField, self).__init__(to, to_field, OneToOneRel, **kwargs)