Ticket #8241: onetoone_admin.8461.diff

File onetoone_admin.8461.diff, 2.7 KB (added by Ilya Semenov, 16 years ago)
  • django/forms/models.py

     
    2828    If commit=True, then the changes to ``instance`` will be saved to the
    2929    database. Returns ``instance``.
    3030    """
    31     from django.db import models
     31    from django.db.models import AutoField, OneToOneField
    3232    opts = instance._meta
    3333    if form.errors:
    3434        raise ValueError("The %s could not be %s because the data didn't"
    3535                         " validate." % (opts.object_name, fail_message))
    3636    cleaned_data = form.cleaned_data
    3737    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)) \
    3939                or not f.name in cleaned_data:
    4040            continue
    4141        if fields and f.name not in fields:
     
    289289            existing_objects[obj.pk] = obj
    290290        saved_instances = []
    291291        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]]
    293293            if self.can_delete and form.cleaned_data[DELETION_FIELD_NAME]:
    294294                self.deleted_objects.append(obj)
    295295                obj.delete()
     
    317317
    318318    def add_fields(self, form, index):
    319319        """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
    322326            form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput)
    323327        super(BaseModelFormSet, self).add_fields(form, index)
    324328
  • django/db/models/fields/related.py

     
    744744    """
    745745    def __init__(self, to, to_field=None, **kwargs):
    746746        kwargs['unique'] = True
    747         kwargs['editable'] = False
     747        if not kwargs.get('primary_key', False):
     748            kwargs['editable'] = False
    748749        if 'num_in_admin' not in kwargs:
    749750            kwargs['num_in_admin'] = 0
    750751        super(OneToOneField, self).__init__(to, to_field, OneToOneRel, **kwargs)
Back to Top