Anonymous Coder
Anonymous Coder

Reputation: 107

Flask error TypeError: Incompatible collection type: str is not list-like

I am getting error when adding data to the table from a view in Flask. The table I am trying to update has ForeignKey relation to another model. However I am trying to update child model. Following is a simple case:

Models:

Parent Model

class Student(db.Model):
   __tablename__='student'
   ....
   package = package = relationship('Package', backref=backref('student'))
   ....
   def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

Child Model

class Package(db.Model):
    __tablename__ = 'package'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    student_id = db.Column(db.Integer, ForeignKey('student.id'))
    stripe_id = db.Column(db.String(45))
    student_email = db.Column(db.String(20))
    subscription_date = db.Column(db.DateTime, default=today)
    expiry_date = db.Column(db.DateTime, default=deadline)
    is_active = db.Column(db.Boolean, default=True)
    planname = relationship('Plan', backref=backref('package'))
    package_price = db.Column(db.Integer)
    coupon = db.Column(db.String(12))

    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

My View:

@app.route('/yearlychargedrec', methods=['GET', 'POST'])
def yearly_charged_rec():

    if not user_authorized():
        return redirect('/')
    # customer
    stripe_token = request.form['stripeToken']
    email = request.form['stripeEmail']

    customer = stripe.Customer.create(
        email=email,
        source=request.form['stripeToken']
    )
    try:
        subscription = stripe.Subscription.create(
            customer=customer.id,
            plan="yearlyrec",
        )

    except stripe.error.CardError as e:
        # The card has been declined
        body = e.json_body
        err = body['error']
    if request.method == 'POST':
        # email = email 

        package = Package(

            is_active=True,
            planname = 'yearlyrec',

        )
        db.session.add(package)
        db.session.commit()

    return render_template('/profile/charge/monthlycharge.html')

Error:

TypeError: Incompatible collection type: str is not list-like

Upvotes: 0

Views: 3057

Answers (1)

metmirr
metmirr

Reputation: 4302

Package and Plan model have a relationship, so the below line

planname = relationship('Plan', backref=backref('package'))

you have in Package model adds an attribute named package to Plan model, and this attribute is reference to a Package not to the Plan so you can not create a package object like that.

The solution is should have same relationship on Plan model let's say your plan class has something like this:

planname= relationship('Package', backref=backref('plan'))

Now you can create a Package object as follows:

plan = Plan(name='yearlyrec')
package = Package(is_active=True, plan=plan)

Of course you can query and use an existing Plan object in this scenario.

Upvotes: 1

Related Questions