1

I am building a location tracking app where a user can be both driver and manager.

There are two different app for driver and manager. User can be both manager and driver at same time.

So, I have a model like:

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)

    def __init__(self, name):
        self.name = name

class Driver(User):
    driving_license = db.Column(db.String)

    def __init__(self, driving_license):
        self.driving_license)

class Manager(User):
    designation = db.Column(db.String)

Is this the correct way to do it or should I design a user class and separate driver and manager class without inheriting from user class. In latter case, I can mark two flags is_driver and is_manager.

If former is correct, than how to write the init function for Driver and Manager by linking it to User class.

1
  • Well, either can be correct depending on your particular application. You do not need to override the default __init__ in most cases; the default __init__ takes keyword arguments and sets them on the instance. Commented May 18, 2016 at 19:01

1 Answer 1

2

You almost definitely do not want to inherit the user model. It will make it very difficult to track individual users because you will have two different effective "User" rows for an individual user. I would personally recommend something like:

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    driver = db.relationship('Driver', uselist=False, back_populates='user')
    manager = db.relationship('Manager', uselist=False, back_populates='user')

class Driver(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    driving_license = db.Column(db.String)
    user_id = db.Column(db.Integer, ForeignKey('user.id'))
    user = db.relationship('User', back_populates='driver')

class Manager(db.Model):
    id = db.Column(db.Integer, primary_key=True) 
    designation = db.Column(db.String)
    user_id = db.Column(db.Integer, ForeignKey('user.id'))
    user = db.relationship('User', back_populates='manager')

Also, you generally don't need to override the __init__ methods. So at this point, we can set our driver/manager fairly easily.

user = User(name='MyName')
db.session.add(user)
db.session.commit()
manager = Manager(designation='Some designation', user=user)
db.session.add(manager)
db.session.commit()
Sign up to request clarification or add additional context in comments.

1 Comment

Works well for me. You can also composite Driver.id or Manager.id with user.id into one field (if you don't want a separate user_id field on the 'children' models) like so: id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) then get rid of 'user_id' columns

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.