diff --git a/flask_admin/model/fields.py b/flask_admin/model/fields.py index ed28c7e88..d49841c94 100644 --- a/flask_admin/model/fields.py +++ b/flask_admin/model/fields.py @@ -119,11 +119,20 @@ def __init__(self, form_class, pk, form_opts=None, **kwargs): self.form_opts = form_opts def get_pk(self): + """Get the primary key value from the form""" + try: + if isinstance(self._pk, (tuple, list)): + return tuple(getattr(self.form, pk).data for pk in self._pk) + + return getattr(self.form, self._pk).data + except AttributeError: + if self._pk not in self.form: + raise AttributeError( + 'Primary key field "%s" is required in inline_models "form_columns". Available form fields: %s' + % (self._pk, self.form._fields.keys()) + ) + raise - if isinstance(self._pk, (tuple, list)): - return tuple(getattr(self.form, pk).data for pk in self._pk) - - return getattr(self.form, self._pk).data def populate_obj(self, obj, name): for name, field in iteritems(self.form._fields):