Hello guys,

I'm building a web service to provide data to our SDK and I'm facing a 
wired problem. I have created 4 different models for my needs, a base class 
that is called *Unit*, two derivatives from the base class *VideoUnit*, 
*QuestionUnit*, and the last class is a derivative of both *VideoUnit* and 
*QuestionUnit*, that is called, with lot of fantasy, *VideoQuestionUnit*.
Everything was working fine till the point I was trying to serialize this 
data to be transmitted with JSON.
I successfully gather JSON from *VideoUnit* and *QuestionUnit *(thanks to 
plaes<http://stackoverflow.com/questions/7102754/jsonify-a-sqlalchemy-result-set-in-flask>)
 
but now I can't figure out why when I try to make the same thing with 
*VideoQuestioUnit 
*the program crash telling me: "*KeyError: 'questions'*"
I can imagine that there is something wrong when *VideoQuestionUnit* try to 
access the "*questions"* data declared in *QuestionUnit*(also my dog get 
that I know), but I don't understand what's wrong with that. 
Did someone have an idea of what I'm doing wrong?


*CODE:*
class Unit(db.Model):
    id = db.Column(db.Integer, unique = True, primary_key = True)
    name = db.Column(db.String(128), index = True, unique = False)
    type = db.Column(db.SmallInteger)
    brand_id = db.Column(db.Integer, db.ForeignKey('brand.id'))
    @property
    def serialize(self):
        print "Serialize on superclass"
        pass

class VideoUnit(Unit):
    uri = db.Column(db.String(256), index = False, unique = False)
    @property
    def serialize(self):
        return {
            'ID'        :   self.id,
            'Name'      :   self.name,
            'Link'      :   self.uri
        }

class QuestionUnit(Unit):
    questions = db.relationship('Question', backref = 'QuestionUnit', lazy 
= 'dynamic')
    @property
    def serialize(self):
        return {
            'ID'        :   self.id,
            'Name'      :   self.name,
            'Questions' :   self.serialize_questions
        }
    @property
    def serialize_questions(self):
        return [item.serialize for item in self.questions]

class VideoQuestionUnit(VideoUnit, QuestionUnit):
    @property
    def serialize(self):
        return {
            'ID'        :   self.id,
            'Name'      :   self.name,
            'Link'      :   self.uri,
            'Questions' :   self.serialize_questions
        }
    @property
    def serialize_questions(self):
        return [item.serialize for item in self.questions]



Thanks in advance,

Enrico

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to