1

I'm looking for convenient way to create ModelView, used to append a number of children to a parent when creating it. For example, I've got models:

class Order(db.Model):
    __tablename__ = 'order'
    id = db.Column(db.Integer, primary_key=True)
    things = db.relationship('OrderList', back_populates='order', lazy='dynamic')

class OrderList(db.Model):
    __tablename__ = 'order_list'
    order_id = db.Column(db.Integer, db.ForeignKey('order.id'), primary_key=True)
    thing_id = db.Column(db.Integer, db.ForeignKey('things.id'), primary_key=True)
    amount = db.Column(db.Numeric(precision=10, scale=3))
    order = db.relationship('Order', back_populates='things', lazy='joined')
    thing = db.relationship('Thing', back_populates='orders', lazy='joined')

class Thing(db.Model):
    __tablename__ = 'things'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128))
    orders = db.relationship('OrderList', back_populates='thing', lazy='dynamic')
    stock = db.Column(db.Numeric(precision=10, scale=3))
    price = db.Column(db.Numeric(precision=10, scale=3))

Simply adding that models to flask-admin, I would get an opportunity to append OrderList instances to Thing or to Order:

enter image description here enter image description here But how do I append children to parent without accessing middle OrderList object? Just like using One-to-many relationship.

Table scheme:

enter image description here

aryndin
  • 591
  • 5
  • 18
  • Why would you want to append `Thing` to `Order` , you haven't defined any relationship there. I might be mistaken , can you explain the question in little more detail. – formatkaka May 27 '16 at 08:46
  • @Siddhant Why did you assume I haven't defined any relationship? Actually, I've used db.relationship to relate Order to OrderList and OrderList to Thing to declare M-to-M relationship. I'll extend my question with table scheme. – aryndin May 27 '16 at 10:13

1 Answers1

1

From what I am understanding , you want to define a many to many relationship on Order and Thing .

Rather than declaring OrderList class , I think you should declare the many to many relation like this

  1. Many to Many

  2. Adding Removing in MtM

This way you can create instances of both Order and Thing and add them to each other to create a Many to Many relationship.

EDIT

You can use Association Proxy to get the work done.

You need to add the following constructor to yourOrderList class

def __init__(self,amount, order, thing):
    self.amount = amount
    self.order = order
    self.thing = thing

Other than this you will have to define association proxy as shown in the link. This should get your work done.

formatkaka
  • 1,278
  • 3
  • 13
  • 27
  • As you can see above, I've already defined MtM relationship using `Assotiation Object` pattern - http://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html#association-object I can't use basic `association_table` pattern since i need store additional data, like `price` and `amount`. Now I need to make flask-admin to allow appending children (things) to parrent (order) while creating a new one without bothering with proxy object. – aryndin May 27 '16 at 11:57
  • ok, supposing i've done with OrderList class, AssotioationProxy and others, how do I now set amount and price of every thing while creating new order? I need to extend default order edit view with additional fields at least. – aryndin Jun 07 '16 at 09:13
  • Please check the documentation link I have provided in my answer. It has been explained there. – formatkaka Jun 07 '16 at 09:29
  • It isn't a problem to append children to a parent using python+sqlalchemy+(optional)AssotiationProxy for me, but problem is to show and then handle List of Fields when creating new parent. Suppose I create new parent, choice a number of children, then i want to describe a number of children through list of field appeared somewhere below. This list looks like (child 1 - price - amount, child 2 - price - amount etc) – aryndin Jun 07 '16 at 09:37