pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
https://flask-admin.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5.69k stars 1.56k forks source link

One-To-Many relationship, Select or Add Inline model Option in the Parent Model View? #2364

Open hasansezertasan opened 1 year ago

hasansezertasan commented 1 year ago

Hey everyone 👋

I've been searching about it and I found that we could use InlineFormAdmin and InlineOneToOneModelConverter for one-to-one inline models and a similar approach for one-to-many models but my goal is to offer an option.

Search and select the child model, if it doesn't exist, then create it while creating the parent model. Is this possible? As far as I searched, I couldn't find any method of doing this.

Let's say that we have two tables, owners and cars. A car can only have one owner but an owner can have multiple cars. Let's say that I have more columns to record in the database and I want to add Cars first with their Owners but what if the owner is already in my database? I may search id and select in on the same create form but what if not, then I should go to the Owner View and add an Owner and then I should come back and edit the Owner, right? My goal is basically add or select an owner when creating a car record.

class Owner(Base):
    id = Column(Integer, primary_key=True)
    date_created = Column(DateTime, default=datetime.utcnow)
    cars = relationship("Car", back_populates="owners")

class Car(Base):
    id = Column(Integer, primary_key=True)
    date_created = Column(DateTime, default=datetime.utcnow)
    plate = Column(String)
    owner_id = Column(Integer, ForeignKey("owner.id"))
    owner = relationship("Owner", back_populates="cars")