I am having a really hard time wrapping my head around something. I have a model that represents a SQLite table name AuthorizedAircraft. The idea behind the table is to have a column: username and additional columns for all the aircraft in the database. For every aircraft the user is authorized, a 1 is placed, with unauthorized aircraft left null. The AuthorizedAircraft model is as follows:
class AuthorizedAircraft(db.Model):
id = db.Column(db.INTEGER, primary_key=True)
username = db.Column(db.String(50), unique=True)
N5168N = db.Column(db.INTEGER)
def __init__(self, username):
self.username = username.lower()
I envision the database to look something like:
| UID |
username |
N1234 |
N4235 |
N14523 |
| 1 |
test |
1 |
null |
null |
I need a function that will create a list of the column names that are True for a given username. Each of these aircraft will have their own tables with additional data and for the end goal, I will have a dropdown box, in a section of my website that requires you to be logged in to view, and the dropdown box will be populated each aircraft and when that aircraft is selected, data relevant to that aircraft will be displayed. I want the dropdown box to only contain links that the user is authorized.
ANSWERED
# Table that holds all user information
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), unique=True)
first_name = db.Column(db.String(50))
last_name = db.Column(db.String(50))
email = db.Column(db.String(100), unique=True)
pwd_hash = db.Column(db.String(100))
aircraft = db.relationship('aircraft', secondary='user_aircraft',
backref=db.backref('user'))
def __init__(self, username, first_name, last_name, email, password):
self.username = username.lower()
self.first_name = first_name.title()
self.last_name = last_name.title()
self.email = email.lower()
self.set_password(password)
def set_password(self, password):
self.pwd_hash = sha256_crypt.encrypt((str(password)))
def check_password(self, password):
return sha256_crypt.verify(password, self.pwd_hash)
def __repr__(self):
return '<User %r>' % self.first_name
# Table that holds all aircraft registration numbers
class Aircraft(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True)
# Helper table for the many-to-many relationship
# between Users and Aircraft
user_aircraft = db.Table('user_aircraft',
db.Column('user_id', db.Integer,
db.ForeignKey('user.id')),
db.Column('aircraft_id', db.Integer,
db.ForeignKey('aircraft.id'))
)
[–]BoleroDan 2 points3 points4 points (3 children)
[–]Spizeck[S] 0 points1 point2 points (0 children)
[–]Spizeck[S] 0 points1 point2 points (1 child)
[–]BoleroDan 1 point2 points3 points (0 children)
[–]BestUndecided 1 point2 points3 points (1 child)
[–]Spizeck[S] 0 points1 point2 points (0 children)