简体   繁体   中英

Flask WTF cannot find a value to populate from the provided obj or input data/defaults

I'm using python Flask with WTForm and I'm having serious issues with the populate_obj method :

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2088, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2070, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1515, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/app/my_data/main.py", line 29, in submit
    form.populate_obj(demande)
  File "/usr/local/lib/python3.9/site-packages/wtforms/form.py", line 95, in populate_obj
    field.populate_obj(obj, name)
  File "/usr/local/lib/python3.9/site-packages/wtforms/fields/core.py", line 877, in populate_obj
    raise TypeError('populate_obj: cannot find a value to populate from the provided obj or input data/defaults')
TypeError: populate_obj: cannot find a value to populate from the provided obj or input data/defaults

Here's my code (nothing fancy):

    form = DemandeForm(data=request.form)
    if form.validate():
        form.populate_obj(demande)
        flash('Enregistré', 'success')
        return redirect(url_for('main.index'))

I could detail all the forms fields, but this would be tedious. My guess is that this as to do with nested forms I'm using:

class DemandeForm(BaseForm):
    AdressePrivee = FormField(AdresseForm)
    AdresseProfessionnelle = FormField(AdresseForm)
    Prestations = FieldList(FormField(PrestationForm), min_entries=1)

So my questions are:

  • why is the error rising?
  • how can I have more explicit error, telling me which fields are raising the issue?
  • how can I solve this?

I'm completely locked right now :(

Thanks for support


EDIT

So I finally managed to confirm that the issue comes from the FormField(AddressField) .

Here's my Demande model extract:

    AdressePrivee = db.relationship(AdressePrivee, backref='Demande', lazy=False, uselist=False)

And in DemandeForm :

    AdressePrivee = FormField(AdresseForm)

Wtform is not happy because demande.AdressePrivee == None , hence self._obj is None in the code and the error rises.

I tried to add default value:

    AdressePrivee = FormField(AdresseForm, default=AdressePrivee())

But then I get an error while saving, as AddressePrivee fields are empty while they shouldn't...

Ain't there a missing option, like nullable: True or something? I'm not sure it makes sense that the error rises, nor how I can handle this situation...

Thanks

Here is the relevant source code for the WTF function, this is where the error is triggered:

def populate_obj(self, obj, name):
    candidate = getattr(obj, name, None)
    if candidate is None:
        if self._obj is None:
            raise TypeError('populate_obj: cannot find a value to populate from the provided obj or input data/defaults')
        candidate = self._obj
        setattr(obj, name, candidate)

    self.form.populate_obj(candidate)

Source

self._obj is assigned a value in the routine process .

So it seems that there is something wrong with the variable you are passing. In fact I don't understand why you are passing the equivalent of current_user to your form. Look here for an example of usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM