更新布尔值的问题

时间:2019-05-05 16:48:47

标签: python sqlalchemy flask-sqlalchemy

在我的烧瓶应用程序中,我创建了两个模型,Car和Place。 问题是,包含布尔值的Place模型在添加新车时不会同时更新。

我已经创建了两个模型(Car和Place)。

我的模型(汽车和地方模型)

class Car(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    mark = db.Column(db.String(25), unique=False, nullable=False)
    model = db.Column(db.String(25), unique=False, nullable=False)
    color = db.Column(db.String(16), unique=False, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
class Place(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    numb = db.Column(db.Integer, unique=True, nullable=False)
    is_available = db.Column(db.Boolean())

放置路线

def make_a_place_unavailable(the_place):
    place_concerned = Place.query.filter_by(numb=the_place).first()
    place_concerned.is_available = False

汽车路线

from project.cars.forms import AdmitCarForm
from project.models import Car, Place
from project import db
from project.places.routes import make_a_place_unavailable

@cars.route('/c_car', methods=['GET', 'POST'])
@login_required
def c_car():
    form = AdmitCarForm()
    available_places = []
    places = Place.query.filter_by(is_available=True)

    # fill out available places list
    for available_place in places:
        available_places.append(available_place)

    # redirect user to home page if all places are unavailable
    if len(available_places) == 0 :
        flash('All places are unavailable. Remove a car or wait')
        return redirect(url_for('main.home'))
    else:
        # choose a random available place to park car
        choosen_place = randint(1, len(available_places))

        if form.validate_on_submit():
            # add car to database
            car = Car(
                mark = form.mark.data,
                model = form.model.data,
                color = form.color.data,
                user_id = current_user.id
            )
            db.session.add(car)

            # make place unavailable
            make_a_place_unavailable(choosen_place)

            # add car to database
            db.session.commit()

            flash(f'Car on parking!')
            return redirect(url_for('main.home'))

    return render_template('c_car.html', form=form, choosen_place=choosen_place)

结果是在数据库中,当通过模板中的表单添加新车时,仅添加了车,而所选位置未在数据库中更新。 我想要的是当我通过表格添加新车时,指示要更新的地方。

1 个答案:

答案 0 :(得分:0)

我没有直接回答您的问题,但我建议您不需要is_available字段。

通过在模型上使用relationship()属性,您可以轻松判断出Place是否可用,而且还可以查看{{1}中的Car },并轻松找到Place在哪个Place中。

Car

我已注释了进行更改的模型,还删除了class User(db.Model): id = db.Column(db.Integer, primary_key=True) class Car(db.Model): id = db.Column(db.Integer, primary_key=True) mark = db.Column(db.String(25), unique=False, nullable=False) model = db.Column(db.String(25), unique=False, nullable=False) color = db.Column(db.String(16), unique=False, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # new relationship fields place = db.relationship('Place', back_populates='car') user = db.relationship('User') class Place(db.Model): id = db.Column(db.Integer, primary_key=True) # As numb is not nullable, and must be unique, you could consider # removing the `id` column above and making `numb` primary key. numb = db.Column(db.Integer, unique=True, nullable=False) # new column car_id = db.Column(db.Integer, db.ForeignKey('car.id')) # new relationship car = db.relationship('Car', back_populates='place', uselist=False)

在这里我创建了一些地方和一个用户进行测试:

Place.is_available

现在,创建一辆汽车并将其放置在一个地方:

# add some places
db.session.add_all([
    Place(id=1, numb=1),
    Place(id=2, numb=2),
    Place(id=3, numb=3)
])
# create a user
me = User()
db.session.add(me)
db.session.commit()