question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

inline_models don't work for one-to-many

See original GitHub issue

I’ve not been able to make inline_models forms for one-to-many, and I think it’s linked to this line: https://github.com/flask-admin/flask-admin/blob/master/flask_admin/contrib/sqla/form.py#L561

class Company(Model):
    id = db.Column(db.Integer, primary_key=True)
    address_id = db.Column(db.Integer, db.ForeignKey('address.id'))
    address = db.relationship('Address', backref='companies')

class Address(Model):
    id = db.Column(db.Integer, primary_key=True)

class CompanyModelView(ModelView):
    inline_models = (Address, )

That gives me the following exception: Exception: Cannot find reverse relation for model <class 'app.models.address.Address'>

Wondering if I’m missing something, or if this is per design ? It seems possible to change the key resolution method there to support the one-to-many.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:21
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
egocommented, Jun 22, 2019

models.py

class User(db.Model):
    id          = db.Column(db.Integer, primary_key=True, autoincrement=True)

class UserProfile(db.Model):
    user_id     = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False)
    user        = db.relationship(User, backref=db.backref("profiles", lazy="dynamic"))

views.py

from flask_admin.model.fields import InlineModelFormField, InlineFormField, InlineFieldList
from wtforms.utils import unset_value

class InlineFieldListA(InlineFieldList):
    def process(self, formdata, data=unset_value):
        res = super().process(formdata, [data])  # hack
        return res

class UserAdmin(base.ModelView):
    model_class = User


class UserProfileAdmin(base.ModelView):
    model_class = UserProfile

    def scaffold_form(self):
       # use native modelview
        form = super().scaffold_form()
        ua = UserAdmin(db.session)
        user_form = ua.get_form()
        form.user = InlineFieldListA(InlineFormField(user_form))
        return form

Wubba lubba dub dub!

1reaction
jminardicommented, Oct 8, 2021

A quick workaround is to apply the inline model to the inverse relationship. For example in OPs original example instead of applying the inline mode to CompanyModelView you would do:

class Address ModelView(ModelView):
    inline_models = (Company, )

While this isn’t exactly what was asked for, it does allow you to achieve the goal of creating both models on one webpage.

Read more comments on GitHub >

github_iconTop Results From Across the Web

one-to-many inline select with django admin - Stack Overflow
It does work, but it will bite you later (for instance, in the first line of Student ). And, class = models.ForeignKey(Class) won't...
Read more >
[Solved]-Showing partial tabular inline models in Django-django
Inline admins (like TabularInline ) are only used when you have a one-to-many relation, which is created by a ForeignKey on the many...
Read more >
Source code for flask_admin.contrib.sqla.form - Flask-Admin
'MANYTOONE': kwargs['allow_blank'] = True if not ... None # Current Unique Validator does not work with multicolumns-pks if not has_multiple_pks(model): ...
Read more >
Model creation, ForeignKey relations and the Django Admin
the Profile Model has a OneToOne relationship with the User model. ... call the full_clean() method of the instance, as simply saving the...
Read more >
In admin, is there a post-commit hook that is executed after all ...
point in the admin save process as save_model(), this also did not work. ... main model and inline models are combined into a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found