Qwait schrieb:
> I am unsure how to insert into a relationship table.
>
> Here is my table setup:
>
> quiz_question_choices = Table('question_choices', metadata,
> Column('question_id', mysql.MSBigInteger(20, unsigned = True),
> ForeignKey('quiz_questions.id', onupdate="CASCADE",
> ondelete="CASCADE")),
> Column('choice_id', mysql.MSBigInteger(20, unsigned = True),
> ForeignKey('quiz_choices.id', onupdate="CASCADE",
> ondelete="CASCADE")),
> )
>
>
> class QuizQuestion(DeclarativeBase):
> __tablename__ = 'quiz_questions'
>
> id = Column(mysql.MSBigInteger(20, unsigned = True), primary_key =
> True, autoincrement = True)
> question = Column(Unicode(200), unique = True)
> correct_choice_id = Column(mysql.MSBigInteger(20, unsigned = True),
> ForeignKey('quiz_choices.id'))
>
> choices = relation('QuizChoice', secondary=quiz_question_choices,
> backref='choice')
>
> class QuizChoice(DeclarativeBase):
> __tablename__ = 'quiz_choices'
>
> id = Column(mysql.MSBigInteger(20, unsigned = True), primary_key =
> True, autoincrement = True)
> text = Column(Unicode(180))
>
> I tried
>
> question = QuizQuestion()
> question.question = kw['question']
> question.correct_choice_id = kw['correct_choice']
> question.choices.append(kw['choice_1'])
> question.choices.append(kw['choice_2'])
> question.choices.append(kw['choice_3'])
>
> But when it gets to "question.choices.append(kw['choice_1'])" I get
> "AttributeError: 'unicode' object has no attribute
> '_sa_instance_state'"
>
> I want this query to insert into the QuizQuestion table and insert the
> quiz_id and choice_id into quiz_question_choices.
>
> kw['choice_1'], kw['choice_2'], kw['choice_3'] are the choice_ids from
> quiz_choices which are created using tw.form's SingleSelectField
>
> Could someone tell me what I am doing wrong and how to fix this ?
>
> TurboGears 2 2.0
> Python 2.5
Please ask these kinds of questions on the normal TG-list. This list is
for development related discussions.
Your problem is simple - getting parameters from a HTTP-post does not
make the id's magically objects.
For this, you'd need either explicit something like this:
choice_1 = QuizChoice.get(int(kw["choice_1"]))
Or you use validation to convert passed values to "real" choices.
Diez
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears Trunk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/turbogears-trunk?hl=en
-~----------~----~----~----~------~----~------~--~---