Code

Ticket #8241: 8241.patch

File 8241.patch, 2.2 KB (added by sciyoshi, 6 years ago)
  • db/models/fields/related.py

     
    726726    """ 
    727727    def __init__(self, to, to_field=None, **kwargs): 
    728728        kwargs['unique'] = True 
    729         kwargs['editable'] = False 
     729        if kwargs.get('parent_link', False):  
     730            kwargs.setdefault('editable', False)  
    730731        if 'num_in_admin' not in kwargs: 
    731732            kwargs['num_in_admin'] = 0 
    732733        super(OneToOneField, self).__init__(to, to_field, OneToOneRel, **kwargs) 
  • forms/models.py

     
    115115            else: 
    116116                # MultipleChoiceWidget needs a list of pks, not object instances. 
    117117                data[f.name] = [obj.pk for obj in f.value_from_object(instance)] 
     118        elif f.primary_key: 
     119            data[f.attname] = f.value_from_object(instance) 
    118120        else: 
    119121            data[f.name] = f.value_from_object(instance) 
    120122    return data 
     
    289291            existing_objects[obj.pk] = obj 
    290292        saved_instances = [] 
    291293        for form in self.initial_forms: 
    292             obj = existing_objects[form.cleaned_data[self.model._meta.pk.attname]] 
     294            obj = existing_objects[form.cleaned_data[self._pk_field_name]] 
    293295            if self.can_delete and form.cleaned_data[DELETION_FIELD_NAME]: 
    294296                self.deleted_objects.append(obj) 
    295297                obj.delete() 
     
    317319 
    318320    def add_fields(self, form, index): 
    319321        """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 
    322             form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput) 
     322        self._pk_field_name = self.model._meta.pk.attname 
     323        form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput) 
    323324        super(BaseModelFormSet, self).add_fields(form, index) 
    324325 
    325326def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.formfield(),