I have a Many-to-Many relationship that I would like to keep track of the 
order. I am wondering if I can automatically populate the module_position 
field based on the current number of AgendaModule's for an Agenda.


class AgendaModule(db.Model):
    __tablename__ = 'agenda_modules'
    module_id = db.Column(db.Integer, db.ForeignKey('modules.id'), 
primary_key=True)
    agenda_id = db.Column(db.Integer, db.ForeignKey('agendas.id'), 
primary_key=True)
    module_position = db.Column(db.Integer)
    # module_position = db.Column(db.Integer, 
default=len(Agenda.query.get(agenda_id).modules) + 1) # NameError: name 
'Agenda' is not defined
    module = db.relationship('Module')

class Agenda(db.Model):
    __tablename__ = 'agendas'
    modules = db.relationship('AgendaModule', backref='agendas', 
order_by=AgendaModule.module_position)

>>> a = Agenda(name='Test')>>> mod = Module.query.all()[0]>>> am = 
>>> AgendaModule()>>> am.module = mod>>> am.module_position = len(a.modules) + 
>>> 1>>> a.modules.append(am)>>> a.modules[<app.agendas.models.AgendaModule 
>>> object at 0x7fe7ccfccd50>]>>> am.module_position1

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to