Code

Opened 6 years ago

Closed 3 years ago

#8133 closed Uncategorized (needsinfo)

BaseModelForm have to populate data not just object_data

Reported by: keizie Owned by: nobody
Component: Forms Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

r8222, django/forms/models.py reads

class BaseModelForm(BaseForm):
    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
                 initial=None, error_class=ErrorList, label_suffix=':',
                 empty_permitted=False, instance=None):
        opts = self._meta
        if instance is None:
            # if we didn't get an instance, instantiate a new one
            self.instance = opts.model()
            object_data = {}
        else:
            self.instance = instance
            object_data = model_to_dict(instance, opts.fields, opts.exclude)
        # if initial was provided, it should override the values from instance
        if initial is not None:
            object_data.update(initial)
        super(BaseModelForm, self).__init__(data, files, auto_id, prefix, object_data,
                                            error_class, label_suffix, empty_permitted)

With this code, populating a ModelForm have different template output for each set of arguments;

  1. form = BasicForm(instance=request.account)

puts None for {% for field in form %} {{ field.data }}

  1. form = BasicForm(request.POST, instance=request.account)

puts appropriate data for the same syntax.

I tried following lines of code just after object_data is done, and template puts data both case.

        if data is None:
            data = {}
            data.update(object_data)

Attachments (0)

Change History (4)

comment:1 Changed 6 years ago by jacob

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

I'm having trouble understanding this ticket, but if I understand correctly you don't like that you see "None" in your template when you use {{field.data}} when passing no form data?

Is that correct?

If not, can you try to explain a bit more clearly what you think the bug is?

comment:2 Changed 6 years ago by keizie

Yes, you're right.
Passing an instance of a model class without additional dict have to fill BoundField.data as well, I suppose.

comment:3 Changed 6 years ago by keizie

Currently (r8419), ModelFormChlid(d, instance=m) populated by d only.
If d don't have appropriate property for ModelFormChild, while m do,
ModelFormChild don't fill that field, and instance just ignored.

comment:4 Changed 3 years ago by lukeplant

  • Resolution set to needsinfo
  • Severity set to Normal
  • Status changed from new to closed
  • Type set to Uncategorized

I think we need more information from the original reporter to try to find out why this is considered a bug.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.