0

I'm having a problem understanding how to serve my data best. I have 2 models, one is record and the other is log, they have a 1 to many relationship respectively. I'd like to serve this using tg's RestController so I can do mysite.com/api/record_id/log So far I have this:

class API(RestController):

    @expose('json')
    def get_all(self):
        records = DB.query(Record).all()
        return dict(records=records)

    @expose('json')
    def get_one(self, record_id):
        try:
            record = DB.query(Record).filter(
                        Record.record_id==record_id).one()
        except NoResultFound:
            abort(404)
        return dict(record=record)

    @expose('json')
    def log(self, record_id):
        try:
            log = DB.query(Log).filter(
                        Log.record_id==record_id).all()
        except NoResultFound:
            abort(404)
        return dict(log=log)

This works, however, if I go to mysite.com/api/log then it maps (as expected) to the log method and complains about the missing variable record_id. How can this be done so the log method is only accessible after the record resource?

Ruben Quinones
  • 2,442
  • 8
  • 26
  • 30
  • have you tried adding a default value to record_id, like record_id=None, and then in your method check the value of record_id, in case it's None, you know that's not valid, you can display an error, or do a redirection, or whatever you wish.. – andrean Sep 26 '12 at 06:08
  • Yes, but for some reason it looks a bit hackish to me. Just wanted to know if there is a cleaner way to do it. – Ruben Quinones Sep 26 '12 at 06:30

0 Answers0