Last active
March 9, 2016 20:52
-
-
Save souzaux/c652508e0ae05adaf7cb to your computer and use it in GitHub Desktop.
Um exemplo de models.py com Relationships many to many
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### Example: https://gist.github.com/souzaux/c652508e0ae05adaf7cb/edit | |
| from catwatch.blueprints.user.models import User | |
| from catwatch.blueprints.device.models import Device | |
| user_device = db.Table('user_device', | |
| db.Column('user_id', db.Integer, db.ForeignKey('users.user_id')), | |
| db.Column('device_id', db.Integer, db.ForeignKey('devices.device_id')) | |
| ) | |
| class User(UserMixin, ResourceMixin, db.Model): | |
| __tablename__ = 'users' | |
| id = db.Column(db.Integer, primary_key=True) | |
| # Relationships. | |
| devices = db.relationship("Device", secondary=user_device, back_populates="devices") | |
| class Device(ResourceMixin, db.Model): | |
| __tablename__ = 'devices' | |
| id = db.Column(db.Integer, primary_key=True) | |
| name = db.Column(db.String(128), nullable=False) | |
| uniqueid = db.Column(db.String(128), unique=True, | |
| nullable=False, | |
| index=True) | |
| status = db.Column(db.String(128)) | |
| # Relationships. | |
| users = db.relationship("User", secondary=user_device, back_populates="users") |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Traceback (most recent call last):
File "/Users/guilherme/.virtualenvs/catwatch/bin/run", line 9, in
load_entry_point('commands==1.0', 'console_scripts', 'run')()
File "/Users/guilherme/.virtualenvs/catwatch/lib/python2.7/site-packages/click/core.py", line 664, in call
return self.main(_args, *_kwargs)
File "/Users/guilherme/.virtualenvs/catwatch/lib/python2.7/site-packages/click/core.py", line 643, in main
with self.make_context(prog_name, args, **extra) as ctx:
File "/Users/guilherme/.virtualenvs/catwatch/lib/python2.7/site-packages/click/core.py", line 560, in make_context
self.parse_args(ctx, args)
File "/Users/guilherme/.virtualenvs/catwatch/lib/python2.7/site-packages/click/core.py", line 951, in parse_args
echo(ctx.get_help(), color=ctx.color)
File "/Users/guilherme/.virtualenvs/catwatch/lib/python2.7/site-packages/click/core.py", line 416, in get_help
return self.command.get_help(self)
File "/Users/guilherme/.virtualenvs/catwatch/lib/python2.7/site-packages/click/core.py", line 772, in get_help
self.format_help(ctx, formatter)
File "/Users/guilherme/.virtualenvs/catwatch/lib/python2.7/site-packages/click/core.py", line 787, in format_help
self.format_options(ctx, formatter)
File "/Users/guilherme/.virtualenvs/catwatch/lib/python2.7/site-packages/click/core.py", line 892, in format_options
self.format_commands(ctx, formatter)
File "/Users/guilherme/.virtualenvs/catwatch/lib/python2.7/site-packages/click/core.py", line 937, in format_commands
cmd = self.get_command(ctx, subcommand)
File "/Users/guilherme/Projetos/website/cli/cli.py", line 38, in get_command
None, None, ['cli'])
File "/Users/guilherme/Projetos/website/cli/commands/cmd_add.py", line 28, in
from catwatch.app import create_app
File "/Users/guilherme/Projetos/website/catwatch/app.py", line 4, in
from catwatch.blueprints.user.models import User
File "/Users/guilherme/Projetos/website/catwatch/blueprints/user/init.py", line 1, in
from catwatch.blueprints.user.views import user
File "/Users/guilherme/Projetos/website/catwatch/blueprints/user/views.py", line 17, in
from catwatch.blueprints.user.models import User
File "/Users/guilherme/Projetos/website/catwatch/blueprints/user/models.py", line 22, in
from catwatch.blueprints.device.models import Device
File "/Users/guilherme/Projetos/website/catwatch/blueprints/device/models.py", line 6, in
from catwatch.blueprints.user.models import User
ImportError: cannot import name User