Dynamic filter on foreignkey
See original GitHub issueI am using Flask-admin and SQLalchemy I want to limit the choices of a foreign key based on a choice of the foreign key of the parent tabeld.
class City(Base):
__tablename__ = 'city'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
def __unicode__(self):
return self.name
class Street(Base):
__tablename__ = 'street'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
city = Column(Integer, ForeignKey(City.id), nullable=False)
city_ref = relationship(City)
def __unicode__(self):
return self.name
class Adress(Base):
__tablename__ = 'adress'
id = Column(Integer, primary_key=True)
familiyname = Column(String, nullable=False)
street = Column(Integer, ForeignKey(Street.id), nullable=False)
street_ref = relationship(Street)
city_ref = relationship("City",
secondary="join(Street,City,Street.city==City.id)",
primaryjoin="and_(Adress.street==Street.id)",
secondaryjoin="City.id == Street.city")
def __unicode__(self):
return self.name
Now i want to add admin model to add an adress for a family. but first i want to select the city and based on that choice i want to filter the available streets How can this be done?
admin.add_view(sqla.ModelView(Adress, db.session))
This also shows the City but the street column is not filtered when a city is chosen.
Issue Analytics
- State:
- Created 9 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Django - Dynamic filter Foreign Key choices on previοus user ...
1 Answer 1 ... The self.q in this view is the query that you're typing in the form. Also don't forget to import...
Read more >is it possible to apply a dynamic filter for a foreign key ...
Hi all. I need to filter a foreign key dropdown menu depending on other attribute value. For example: On Employee ProfitCenter dropdown menu...
Read more >Dynamic Data Foreign Key Filter - how to limit the # of ... - MSDN
Use [Display(AutoGenerateFilter=false)] attribute for foreign key columns that you don't want to display the drop-down list filters. Wednesday, November 24, ...
Read more >Django Admin - dynamic select options with foreignkey and ...
Note: One supplier can have multiple substrates. Simply, filtering supplier returns multiple query set each with a different substrate ( ...
Read more >How to apply dynamic filter for the GridGrouping control with ...
To apply dynamic filter to the Grid Grouping control that has foreign key relation, you need to use the GridDynamicFilter object to wire...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I found a more elegant way to achieve the same goal:
I hope it helps the next person who find this issue on Google.
I have found a solution to this by using
on_form_prefill
together withquery_factory
, here are the stepson_form_prefill
, and in that method, you can get the current object being edited, so you can define the query_factory of another field based on the current defined field, code shown below:And here is the definition of the query factory in the model:
By this way, we could control which record will be shown in the purchase order line list based on parameter po_id, and the value of po_id is passed to the query factory function in on_form_prefill.