[Flask] Updating database fields from views

Tom Vaughan thomas.david.vaughan at gmail.com
Sat Feb 25 11:18:02 EST 2017


On Sat, Feb 25, 2017 at 12:50 PM, Anonymous Coder
<anonymouscodar at gmail.com> wrote:
> Tried everything once again from previous practices but now after removing
> the line it says:
>
> AttributeError: 'dict' object has no attribute 'session'

Alright. It seems I scanned over your examples too hastily. In "File
#1" you have:

    db = {
        'users': {
            "test": ("test", "test")
        },
        'session': {}
    }

Then in "File #2" you have:

    db = SQLAlchemy(app)

So, in two places you declare a variable called db. The variable db in
File #1 is a dict. The variable db in File #2 is an instance of
SQLAlchemy. I'd probably rename one of the two at least so it's clear
which is which.

The error you see is because you're using the db dict from File #1 and
trying to access an attribute called "session" (which I assume is a
method that exists on the db instance of SQLAlchemy). I think what you
want to do is import db from File #2 not File #1. I see the the db
dict also contains a key called session. If that's what you really
want to use then the correct syntax is db["session"] not db.session.


>
> On Sat, Feb 25, 2017 at 3:09 PM, Tom Vaughan
> <thomas.david.vaughan at gmail.com> wrote:
>>
>> On Sat, Feb 25, 2017 at 12:07 PM, Anonymous Coder
>> <anonymouscodar at gmail.com> wrote:
>> > You mean I should delete following line?
>> >
>> > db='db'
>>
>> Yes.
>>
>>
>> >
>> > On Sat, Feb 25, 2017 at 2:41 PM, Tom Vaughan
>> > <thomas.david.vaughan at gmail.com> wrote:
>> >>
>> >> On Sat, Feb 25, 2017 at 11:35 AM, Anonymous Coder
>> >> <anonymouscodar at gmail.com> wrote:
>> >> > I think I found it. I really am sorry that I was not clearly lay down
>> >> > source
>> >> > code reflecting the issue and thanks for the patience. In __init__
>> >> > file
>> >> > the
>> >> > previous developer has something like below:
>> >> >
>> >> > #File1
>> >> > login_manager = LoginManager()
>> >> > login_manager.init_app(app)
>> >> >
>> >> > db = {
>> >> >     'users': {
>> >> >         "test": ("test", "test")
>> >> >     },
>> >> >     'session': {}
>> >> > }
>> >> > app.config['DEBUG'] = True
>> >> > app.config['WTF_CSRF_ENABLED'] = False
>> >> > app.secret_key =
>> >> >
>> >> > '\x06\x94\xcf\xaf\xaeB&\xd1s\xa8ZGU\xd2J\xf3\xd6\x12(\xbd\xf5\xc3\x858'
>> >> >
>> >> > db = 'db'
>> >>
>> >> ^^^ Delete this line completely.
>> >>
>> >>
>> >> >
>> >> > Then in another file he declared the database like below:
>> >> >
>> >> > #File 2
>> >> > import os
>> >> > import oursql
>> >> > from flask import Flask
>> >> >
>> >> > app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://'+ 'root' \
>> >> > + ':' + 'test123'+'@'\
>> >> > + 'localhost' + '/students'
>> >> > db = SQLAlchemy(app)
>> >> > def get_connection():
>> >> >     return oursql.connect(host='localhost', port='3306',
>> >> >                           user='root',
>> >> >                           passwd='test123', db='students')
>> >> >
>> >> > Now when I need to create any model and need to add tables I do this
>> >> > in
>> >> > shell:
>> >> >
>> >> > from file2 import db
>> >> > from somemodelfile import MyNewModel
>> >> > db.create_all()
>> >> >
>> >> > That is how it adds tables. But still I am confuse that why it is not
>> >> > letting me update fields.
>> >> >
>> >> > Except above code there is no representation of data related to how
>> >> > db
>> >> > needs
>> >> > to be handled. All the file inside project call database from file1
>> >> > but
>> >> > in
>> >> > shell I can't use create_db(). When in shell I do like below:
>> >> >
>> >> >>>>from file1 import db
>> >> >>>>db
>> >> > 'db' #This is the output
>> >> >
>> >> >
>> >> >
>> >> > On Sat, Feb 25, 2017 at 1:50 PM, Tom Vaughan
>> >> > <thomas.david.vaughan at gmail.com> wrote:
>> >> >>
>> >> >> But how exactly is it imported? Where is it assigned a value? The
>> >> >> problem seems to be that db is not an instance of a Database object,
>> >> >> but rather it has been assigned a string. For example:
>> >> >>
>> >> >> $ python3
>> >> >> Python 3.5.2 (default, Nov 17 2016, 17:05:23)
>> >> >> [GCC 5.4.0 20160609] on linux
>> >> >> Type "help", "copyright", "credits" or "license" for more
>> >> >> information.
>> >> >> >>> db = "foobar"
>> >> >> >>> db.session
>> >> >> Traceback (most recent call last):
>> >> >>   File "<stdin>", line 1, in <module>
>> >> >> AttributeError: 'str' object has no attribute 'session'
>> >> >>
>> >> >>
>> >> >>
>> >> >> On Sat, Feb 25, 2017 at 9:40 AM, Anonymous Coder
>> >> >> <anonymouscodar at gmail.com> wrote:
>> >> >> > It is imported from another file.
>> >> >> >
>> >> >> > On Sat, Feb 25, 2017 at 11:51 AM, Gergely Polonkai
>> >> >> > <gergely at polonkai.eu>
>> >> >> > wrote:
>> >> >> >>
>> >> >> >> The error message you showed us most probably references the db
>> >> >> >> variable.
>> >> >> >> Is db a global variable in the same file? Or is it imported from
>> >> >> >> another
>> >> >> >> module? Can you check its value?
>> >> >> >>
>> >> >> >>
>> >> >> >> On Sat, Feb 25, 2017, 12:46 Anonymous Coder
>> >> >> >> <anonymouscodar at gmail.com>
>> >> >> >> wrote:
>> >> >> >>>
>> >> >> >>> Just a clarity that the stackoverflow post has nothing to do
>> >> >> >>> with
>> >> >> >>> the
>> >> >> >>> question. That problem I got solved. I just gave the reference
>> >> >> >>> to
>> >> >> >>> show
>> >> >> >>> you
>> >> >> >>> how my models are defined.
>> >> >> >>> Thanks
>> >> >> >>>
>> >> >> >>>
>> >> >> >>> On Sat, Feb 25, 2017 at 10:26 AM, Anonymous Coder
>> >> >> >>> <anonymouscodar at gmail.com> wrote:
>> >> >> >>>>
>> >> >> >>>> I am having trouble understanding how to manipulate database in
>> >> >> >>>> Flask
>> >> >> >>>> from views. Rest of topics I was able to cover pretty quickly
>> >> >> >>>> in
>> >> >> >>>> Flask. I am
>> >> >> >>>> a new user so help is much appreciated.
>> >> >> >>>>
>> >> >> >>>> Following is the link for details guys from one of my post at
>> >> >> >>>> stackoverflow. Thank a lot for help guys.
>> >> >> >>>>
>> >> >> >>>>
>> >> >> >>>>
>> >> >> >>>>
>> >> >> >>>>
>> >> >> >>>> http://stackoverflow.com/questions/42451870/flask-error-typeerror-incompatible-collection-type-str-is-not-list-like/42452448?noredirect=1#comment72048738_42452448
>> >> >> >>>>
>> >> >> >>>>
>> >> >> >>>>
>> >> >> >>>> On Sat, Feb 25, 2017 at 8:43 AM, Gergely Polonkai
>> >> >> >>>> <gergely at polonkai.eu>
>> >> >> >>>> wrote:
>> >> >> >>>>>
>> >> >> >>>>> Where does your db variable come from?
>> >> >> >>>>>
>> >> >> >>>>>
>> >> >> >>>>> On Sat, Feb 25, 2017, 08:36 Anonymous Coder
>> >> >> >>>>> <anonymouscodar at gmail.com>
>> >> >> >>>>> wrote:
>> >> >> >>>>>>
>> >> >> >>>>>> I am having trouble with updating database tables from flask
>> >> >> >>>>>> view.
>> >> >> >>>>>> Following is the view:
>> >> >> >>>>>>
>> >> >> >>>>>> @app.route('/yearlychargedrec', methods=['GET', 'POST'])
>> >> >> >>>>>> def yearly_charged_rec():
>> >> >> >>>>>>
>> >> >> >>>>>>     if not user_authorized():
>> >> >> >>>>>>         return redirect('/')
>> >> >> >>>>>>     # customer
>> >> >> >>>>>>     if request.method == 'POST':
>> >> >> >>>>>>         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",
>> >> >> >>>>>>             )
>> >> >> >>>>>>
>> >> >> >>>>>>             package = Package(
>> >> >> >>>>>>
>> >> >> >>>>>>                 is_active=True,
>> >> >> >>>>>>                 planname = 'yearlyrec',
>> >> >> >>>>>>
>> >> >> >>>>>>             )
>> >> >> >>>>>>             db.session.add(package)
>> >> >> >>>>>>             db.session.commit()
>> >> >> >>>>>>
>> >> >> >>>>>>         except stripe.error.CardError as e:
>> >> >> >>>>>>             # The card has been declined
>> >> >> >>>>>>             body = e.json_body
>> >> >> >>>>>>             err = body['error']
>> >> >> >>>>>>
>> >> >> >>>>>>     return
>> >> >> >>>>>> render_template('/profile/charge/monthlycharge.html')
>> >> >> >>>>>>
>> >> >> >>>>>> Error I get is:
>> >> >> >>>>>>
>> >> >> >>>>>> AttributeError: 'str' object has no attribute 'session'
>> >> >> >>>>>>
>> >> >> >>>>>> Please advise.
>> >> >> >>>>>> _______________________________________________
>> >> >> >>>>>> Flask mailing list
>> >> >> >>>>>> Flask at python.org
>> >> >> >>>>>> https://mail.python.org/mailman/listinfo/flask
>> >> >> >>>>
>> >> >> >>>>
>> >> >> >>>
>> >> >> >
>> >> >> >
>> >> >> > _______________________________________________
>> >> >> > Flask mailing list
>> >> >> > Flask at python.org
>> >> >> > https://mail.python.org/mailman/listinfo/flask
>> >> >> >
>> >> >
>> >> >
>> >
>> >
>
>


More information about the Flask mailing list