Skip to content

Instantly share code, notes, and snippets.

@alvmgdev
Forked from cyrexcyborg/relations.py
Created August 21, 2019 10:26
Show Gist options
  • Save alvmgdev/2bf8ab90e5099f8bbd4b24fa352c548f to your computer and use it in GitHub Desktop.
Save alvmgdev/2bf8ab90e5099f8bbd4b24fa352c548f to your computer and use it in GitHub Desktop.

Revisions

  1. @cyrexcyborg cyrexcyborg revised this gist Jun 26, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion relations.py
    Original file line number Diff line number Diff line change
    @@ -53,7 +53,7 @@ class Image(db.Model):

    # nothing special was need in Image, all circular dependencies were solved in Product


    # Need to implement custom Image list
    class ProductView(ModelView):
    def __init__(self, session, **kwargs):
    super(ProductView, self).__init__(Product, session,**kwargs)
  2. @cyrexcyborg cyrexcyborg revised this gist Jun 26, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions relations.py
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@
    # Many thanks to http://stackoverflow.com/users/400617/davidism
    # This code under "I don't care" license
    # Take it, use it, learn from it, make it better.
    # Start this from cmd or shell or whatever
    # Go to favourite browser and type localhost:5000/admin
    import sys
    from flask import Flask
    from flask.ext.sqlalchemy import SQLAlchemy
  3. @cyrexcyborg cyrexcyborg revised this gist Jun 26, 2014. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion relations.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    # -*- coding: utf-8 -*-
    # Many thanks to http://stackoverflow.com/users/400617/davidism
    # Comments in tables by @davidism too.
    # This code under "I don't care" license
    # Take it, use it, learn from it, make it better.
    import sys
    @@ -15,10 +14,17 @@

    # Create in-memory database
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.sqlite'

    # Set echo for debug purposes
    app.config['SQLALCHEMY_ECHO'] = True

    # Create db instance
    db = SQLAlchemy(app)

    # Create admin instance
    admin = Admin(app)


    class Product(db.Model):
    __tablename__ = 'product'

    @@ -45,6 +51,7 @@ class Image(db.Model):

    # nothing special was need in Image, all circular dependencies were solved in Product


    class ProductView(ModelView):
    def __init__(self, session, **kwargs):
    super(ProductView, self).__init__(Product, session,**kwargs)
    @@ -58,8 +65,12 @@ def __init__(self, session, **kwargs):
    admin.add_view(ImageView(db.session))

    if __name__ == '__main__':
    # Create tables
    db.create_all()

    # Run in debug mode
    app.debug = True

    # Go!
    app.run()

  4. @cyrexcyborg cyrexcyborg revised this gist Jun 26, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions relations.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    # -*- coding: utf-8 -*-
    # Many thanks to http://stackoverflow.com/users/400617/davidism
    # Comments in tables by @davidism too.
    # This code under "I don't care" license
    # Take it, use it, learn from it, make it better.
    import sys
  5. @cyrexcyborg cyrexcyborg revised this gist Jun 26, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions relations.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    # -*- coding: utf-8 -*-
    # Many thanks to http://stackoverflow.com/users/400617/davidism
    # This code under "I don't care" license
    # Take it, use it, learn from it, make it better.
    import sys
    from flask import Flask
    from flask.ext.sqlalchemy import SQLAlchemy
  6. @cyrexcyborg cyrexcyborg created this gist Jun 26, 2014.
    61 changes: 61 additions & 0 deletions relations.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    # -*- coding: utf-8 -*-
    import sys
    from flask import Flask
    from flask.ext.sqlalchemy import SQLAlchemy
    from flask.ext.admin import Admin
    from flask.ext.admin.contrib.sqla import ModelView

    app = Flask(__name__)

    app.config['SECRET_KEY'] = '123456790'

    # Create in-memory database
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.sqlite'
    app.config['SQLALCHEMY_ECHO'] = True
    db = SQLAlchemy(app)
    admin = Admin(app)

    class Product(db.Model):
    __tablename__ = 'product'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode(200), nullable=False)

    # cover image foreign key
    # use_alter=True along with name='' adds this foreign key after Image has been created to avoid circular dependency
    cover_id = db.Column(db.Integer, db.ForeignKey('image.id', use_alter=True, name='fk_product_cover_id'))

    # cover image one-to-one relationship
    # set post_update=True to avoid circular dependency during
    cover = db.relationship('Image', foreign_keys=cover_id, post_update=True)

    class Image(db.Model):
    __tablename__ = 'image'

    id = db.Column(db.Integer, primary_key=True)
    path = db.Column(db.String(200), nullable=False)
    product_id = db.Column(db.Integer, db.ForeignKey(Product.id))

    # product gallery many-to-one
    product = db.relationship(Product, foreign_keys=product_id, backref='images')

    # nothing special was need in Image, all circular dependencies were solved in Product

    class ProductView(ModelView):
    def __init__(self, session, **kwargs):
    super(ProductView, self).__init__(Product, session,**kwargs)


    class ImageView(ModelView):
    def __init__(self, session, **kwargs):
    super(ImageView, self).__init__(Image, session,**kwargs)

    admin.add_view(ProductView(db.session))
    admin.add_view(ImageView(db.session))

    if __name__ == '__main__':
    db.create_all()
    app.debug = True

    app.run()