bluefish
bluefish

Reputation: 21

How to define a base_filter based on model content? (Flask-AppBuilder)

I have a simple Department-Employee-Model. Each employee belongs to a single department. In addition, each employee can be also related (affiliated) to one or multiple departments. The respective model in flask-appbuilder looks as follows:

class Department(Model):
    __tablename__ = "Department"
    id = Column(Integer, primary_key=True)
    name = Column(String(120))

    def __repr__(self):
        return self.name

class Employee(Model):
    __tablename__ = "Employee"
    id = Column(Integer, primary_key=True)
    name = Column(String(120))
    departmentId = Column(Integer, ForeignKey("Department" + ".id"))
    department = relationship("Department")

    relatedDepartments = relationship("Department", secondary=mapEmployee_Department, backref="Employee")

    def __repr__(self):
        return self.name

and the respective many-to-any relationship as follows:

mapEmployee_Department = Table("Employee2Department", Model.metadata,
                             Column('id', Integer, primary_key=True),
                             Column('employeeId', Integer, ForeignKey("Employee" + ".id")),
                             Column('departmentId', Integer, ForeignKey("Department" + ".id"))
                            )

The department view can a) list the departments and b) for a selected department the employees of this department.

class DepartmentView(ModelView):
    datamodel = SQLAInterface(Department)
    related_views = [EmployeeView,RelatedEmployeesView ]
    list_widget = ListLinkWidget

This is working fine.

I now want to add a second related view, that - for a selected department - only shows the related employees of this department. For this I define the view as follows:

class RelatedEmployeesView(ModelView):
    datamodel = SQLAInterface(Employee)
    list_widget = ListLinkWidget
    list_columns = ["name","relatedDepartments" ]

    list_title="XXX Related Employees "
    list_widget = ListLinkWidget

    base_filters = [['relatedDepartments', ????, ????]]
    # show employees with employee.relatedDepatments contains "shown department"

Question: I assume base_filters is the right approach for this. How do I define this base_filter to also analyze the data from the currently shown department?

Upvotes: 0

Views: 13

Answers (0)

Related Questions