I'm following the Flask Mega Tutorial by Miguel Grinberg. He does a great job of focusing on logging users in / out, and dealing with adding content (blog posts), but it's challenging to extrapolate simple CRUD operations from the tutorial. The focus seems to be on adding data (user logins, new blog posts), rather than editing existing data.
I've currently got a Company model in models.py, with a method that I think returns a Company object based on the id provided:
class Company(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(120), index = True)
def load_company_by_id(id):
return Company.query.get(int(id))
def __repr__(self):
return '<Company %r>' % (self.name)
In my view, I have:
from flask import render_template, flash, redirect, request
from app import app
from forms import CompanyForm
from models import Company
...
...
@app.route('/company/edit/<id>', methods=['GET','POST'])
def company_edit(id):
company = Company.load_company_by_id(id)
form = CompanyForm(obj=company)
return render_template('company_form.html', form = form)
I'm getting an error: TypeError: unbound method load_company_by_id() must be called with Company instance as first argument (got unicode instance instead). It's not clear to me why a method defined by me would be expecting more arguments than I designed it to expect.