Ticket #8562: 8562_primary_uniques_testupdate.diff
File 8562_primary_uniques_testupdate.diff, 2.4 KB (added by , 16 years ago) |
---|
-
django/forms/models.py
96 96 the ``fields`` argument. 97 97 """ 98 98 # avoid a circular import 99 from django.db.models.fields.related import ManyToManyField, OneToOneField 99 from django.db.models.fields.related import ManyToManyField, OneToOneField, ForeignKey 100 100 opts = instance._meta 101 101 data = {} 102 102 for f in opts.fields + opts.many_to_many: … … 117 117 data[f.name] = [obj.pk for obj in f.value_from_object(instance)] 118 118 elif isinstance(f, OneToOneField): 119 119 data[f.attname] = f.value_from_object(instance) 120 data[f.name] = data[f.attname] 121 elif isinstance(f, ForeignKey) and f.primary_key and f.unique: 122 data[f.attname] = f.value_from_object(instance) 123 data[f.name] = data[f.attname] 120 124 else: 121 125 data[f.name] = f.value_from_object(instance) 122 126 return data … … 319 323 320 324 def add_fields(self, form, index): 321 325 """Add a hidden field for the object's primary key.""" 322 if self.model._meta.pk.auto_created: 326 # include both edges ot OneToOne Primary key, and 327 # ForeignKey, primary key, unique .. which is a OneToOne in reality 328 from django.db.models.fields.related import OneToOneField, ForeignKey 329 if self.model._meta.pk.auto_created or (self.model._meta.pk.primary_key \ 330 and ((isinstance(self.model._meta.pk, OneToOneField)) or \ 331 (isinstance(self.model._meta.pk, ForeignKey) and self.model._meta.pk.unique))): 323 332 self._pk_field_name = self.model._meta.pk.attname 324 333 form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput) 325 334 super(BaseModelFormSet, self).add_fields(form, index) -
tests/modeltests/model_forms/models.py
811 811 >>> bw = BetterWriter(name=u'Joe Better') 812 812 >>> bw.save() 813 813 >>> sorted(model_to_dict(bw).keys()) 814 ['id', 'name', 'writer_ptr _id']814 ['id', 'name', 'writer_ptr', 'writer_ptr_id'] 815 815 816 816 # PhoneNumberField ############################################################