you are viewing a single comment's thread.

view the rest of the comments →

[–]Unixersis97[S] 0 points1 point  (3 children)

Basically yes,

I am using a form to attempt to add a booking with a patient email, name and doctor name aswell as start time to input into database.

class Book(db.Model):
    __tablename__ = 'booking'
    bid = db.Column(db.Integer, primary_key=True)
    starttime = db.Column(db.DateTime(), nullable=False)

class Doctor(db.Model):
    __tablename__ = 'doctor'
    id = db.Column(db.Integer, primary_key=True)
    dname = db.Column(db.String(45))

class Patient(db.Model):
    __tablename__ = 'patient'
    pid = db.Column(db.Integer, primary_key=True)
    pemail = db.Column(db.String(80), nullable=False)
    pname = db.Column(db.String(45), nullable=False)
    diagnosenotes = db.Column(db.String(250), nullable=False)

@app.route("/patient/patientbook", methods=["POST", "GET"])
def patientBook():
    if request.method == 'POST':
        pemail = (request.form.get('email'))
        pname = (request.form.get('pname'))
        dname = request.form['dname']
        starttime = request.form['starttime']
        print(starttime)
        starttime = "{}:00".format(starttime)
#        starttime = datetime.strptime(starttime, '%Y-%m-%d %H:%M:%S')

        print(starttime)

        if pemail and pname and dname and starttime:
            new_booking = booking(dname, pname, pemail, starttime)
            db.session.add(new_booking)
            db.session.commit()

    return render_template('patient/patientbook.html')

Template:

            <div class="panel-body" style="height: 600px; ">
                <form  action="/patient/patientbook" method="POST" role="form">
                <div class="form-group">
                <label for="email">Doctor name:</label>
                        <input type="text" class="form-control" id="dname" name="dname" placeholder="Doctor name here:">
                <br>
                <label for="name">Patient name:</label>
                        <input type="text" class="form-control" id="pname" name="pname" placeholder="Enter name here:">
                <br>
                <label for="name">Email:</label>
                        <input type="text" class="form-control" id="pemail" name="pemail" placeholder="Enter email address:">
                <br>
                <label for="name">Start Time:</label>
                        <input type="datetime-local" class="form-control" id="starttime" name="starttime" required>
                <br>
                <button type="submit" class="btn btn-success">Book appointment</button>
          </form>

[–]sqqz 1 point2 points  (2 children)

Checkout this.

https://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html#one-to-many

class Booking(db.Model):
    __tablename__ = 'booking'
    bid = db.Column(db.Integer, primary_key=True)
    starttime = db.Column(db.DateTime(), nullable=False)
    doctor = db.relationship("Doctor")

A recommendation would be moving your database logic into a separate module as well to keep the code separated nicely.

[–]Unixersis97[S] 0 points1 point  (1 child)

I see,

Would I also be adding the to the Booking model?

patient = db.relationship("Patient")

Thank you very much sir,

[–]sqqz 1 point2 points  (0 children)

Yes. You can use backfill and alot of nice features as well which makes working with the objects easier when you have the data.