Ticket #7938: 7938_force_pk_presentation.diff

File 7938_force_pk_presentation.diff, 2.6 KB (added by magneto, 16 years ago)

Force the display of editable=false, pk=true for inline models (the field ends up being hidden)

  • forms/models.py

     
    9090    opts = model._meta
    9191    field_list = []
    9292    for f in opts.fields + opts.many_to_many:
    93         if not f.editable:
     93        if not f.editable and not f.primary_key:
    9494            continue
    9595        if fields and not f.name in fields:
    9696            continue
     
    120120    opts = model._meta
    121121    field_list = []
    122122    for f in opts.fields + opts.many_to_many:
    123         if not f.editable:
     123        if not f.editable and not f.primary_key:
    124124            continue
    125125        if fields and not f.name in fields:
    126126            continue
     
    138138    Returns a Form class for the given list of Django database field instances.
    139139    """
    140140    fields = SortedDict([(f.name, f.formfield())
    141                          for f in field_list if f.editable])
     141                         for f in field_list if f.editable or f.primary_key])
    142142    return type('FormForFields', (BaseForm,), {'base_fields': fields})
    143143
    144144
     
    161161    opts = instance._meta
    162162    data = {}
    163163    for f in opts.fields + opts.many_to_many:
    164         if not f.editable:
     164        if not f.editable and not f.primary_key:
    165165            continue
    166166        if fields and not f.name in fields:
    167167            continue
     
    195195    field_list = []
    196196    opts = model._meta
    197197    for f in opts.fields + opts.many_to_many:
    198         if not f.editable:
     198        if not f.editable and not f.primary_key:
    199199            continue
    200200        if fields and not f.name in fields:
    201201            continue
     
    378378
    379379    def add_fields(self, form, index):
    380380        """Add a hidden field for the object's primary key."""
    381         self._pk_field_name = self.model._meta.pk.attname
    382         form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput)
     381        if self.model._meta.has_auto_field:
     382            self._pk_field_name = self.model._meta.pk.attname
     383            form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput)
     384        elif self.model._meta.pk.attname and not self.model._meta.pk.editable:
     385               # must have the PK in the inline edits, otherwise, it cannot
     386               # save them ..  a generic Field as it can be any unique type
     387            self._pk_field_name = self.model._meta.pk.attname
     388            form.fields[self._pk_field_name] = Field(required=False, widget=HiddenInput)
    383389        super(BaseModelFormSet, self).add_fields(form, index)
    384390
    385391def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.formfield(),
Back to Top