32563

Display first element of a one-to-many relationship in Flask-Admin

Question:

I'm currently using Flask-Admin to create an admin screen but I'm running into a roadblock. Suppose I have two classes that look like this:

class Part(db.Model): id = db.Column(db.Integer, primary_key=True, unique=True) part_data = db.relationship("PartHistory", backref="part") name = db.Column(db.String(255)) class PartHistory(db.Model): id = db.Column(db.Integer, primary_key=True, unique=True) part_id = db.Column(db.Integer, db.ForeignKey('part.id')) date_checked = db.Column(db.Date)

Part has a one-to-many relationship on PartHistory. I'd like to create a view that shows the Part name in one column and the first date_checked in the other column. I tried something like this:

column_list = ("name", "part_data[0].date_checked")

But that doesn't seem to work. It also seems like I'm going about this the wrong way. Any tips would be appreciated.

Answer1:

One way would be to use <a href="http://docs.sqlalchemy.org/en/rel_0_9/orm/mapper_config.html#using-column-property" rel="nofollow">column-property</a> to define a computed column which would be populated with the field you need. Below I used a min value, but you can construct arbitrary query to get the first by ID (best way to achieve that would be backend-specific):

class Part(db.Model): # ... date_checked = db.column_property( db.select([db.func.min(PartHistory.date_checked)]).\ where(PartHistory.part_id==id).\ correlate_except(PartHistory) )

<em>Note that you might not be able to sort by this column though.</em>

Recommend

  • How to insert a new field into a table which related to another table?
  • join two tables and show all records
  • How do I update related object on update in SQLAlchemy?
  • Compare dates between 2 different rows and columns
  • Configuring cascade delete with EF7
  • SQLAlchemy: Any constraint to check one of the two columns is not null?
  • Assign input form to a variable
  • Ruby on Rails: Selectize.js with Cocoon
  • Filtering on reverse relations
  • Populate CheckboxSelectMultiple with existing data from django model form
  • mysql not unique auto increment, primary key two fields
  • for loop iteration in django
  • can't make autocomplete_light filter taggit tags based on request user
  • NoReverseMatch at / Reverse error with urlresolvers, get_absolute_url()
  • Take data from different tables and display it in View Index in mvc4
  • Implement a writable serializer for multilevel nested relationships in django rest framework
  • Entity framework - Foreign key - data annotation
  • How to determine the association between a VB6 app and an exe instanced with CreateObject()
  • Django ORM Left Join With GROUP BY and SUM
  • How to specify a multi-column UNIQUE constraint in code-first Entity Framework fluent API
  • Detaching entity along with referenced entities
  • Find unique tuples in a relation represented by a BDD
  • Slowly changing dimension - What is Pure type 6 implementation
  • SQL append distinct values from two columns and two tables
  • Hibernate: Inheritance and relationship mapping + generics
  • Django: ORDER BY DESC on FloatField puts null values top?
  • CoreData basics – to-many relationship array data
  • Should I use composite primary keys in Grails?
  • Z3: Convert between FP and BitVector?
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Groovy: Unexpected token “:”
  • Replace value with Factor in r data.table
  • How to access EntityManager inside Entity class in EJB3
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Counter field in MS Access, how to generate?
  • Convert array of 8 bytes to signed long in C++
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?