• flask反向引用


    反向引用

    模型字段名

    class Brand(BaseModel,db.Model):
    __tablename__ = 'brand'
    # id=db.Column(db.Integer,primary_key=True)
    brand_name=db.Column(db.String(20))
    brand_style=db.Column(db.String(30))
    brand_style_detail=db.Column(db.String(50))
    cars=db.relationship('Car',backref='brand')
    def __repr__(self):
    return self.brand_name
    1
    2
    3
    4
    5
    6
    7
    8
    9
    class Car(BaseModel,db.Model):
    __tablename__='sc_cars'
    # id=db.Column(db.Integer,primary_key=True)
    user_id=db.Column(db.Integer,db.ForeignKey('sc_users.id'))
    brand_id=db.Column(db.Integer,db.ForeignKey('brand.id'))
    price= db.Column(db.Integer,default=0,nullable=False)
    car_age=db.Column(db.Integer) #age of the car
    car_style=db.Column(db.String(10))#style of the car
    car_gearbox=db.Column(db.Integer,default=0)#0 for hand,1 for auto
    car_distance=db.Column(db.Integer)
    car_displacement=db.Column(db.Float)
    car_register_time=db.Column(db.DateTime)
    car_num=db.Column(db.String(100))
    car_color=db.Column(db.String(10))
    car_oil_type=db.Column(db.String(10))
    car_emission_standard=db.Column(db.String(10))
    seat_num=db.Column(db.Integer)
    transfer_time=db.Column(db.Integer)
    inspect_annually=db.Column(db.String(10))
    traffic_compulsory_insurance=db.Column(db.String(10))
    commercial_insurance=db.Column(db.String(10))
    images=db.relationship('Carimg',backref='img')
    index_image_url=db.Column(db.String(100))

    orders=db.relationship('Order',backref='orders')

    def to_list_dict(self):
    # newdict = {}
    # brand_obj = each.brand
    # newdict['brand'] = brand_obj.brand_name
    # newdict['carstyle'] = brand_obj.brand_style
    # newdict['carstyle_detail'] = brand_obj.brand_style_detail
    # newdict['car_register_time'] = each.car_register_time
    # newdict['price'] = each.price
    # newdict['car_distance'] = each.car_distance

    car_obj_dict={
    'brand':self.brand.brand_name,
    'carstyle':self.brand.brand_style,
    'carstyle_detail':self.brand.brand_style_detail,
    'car_register_time':self.car_register_time,
    'price':self.price,
    'car_distance':self.car_distance
    }
    return car_obj_dict
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45

    ---------------------

  • 相关阅读:
    二维RMQ问题
    乘法逆元的一些求法
    对于一些小的数学的方法的一些记录
    第一次举办比赛记
    牛客网比赛-Wannafly挑战赛27
    [HEOI2012]Akai的数学作业-题解
    线性基简单学习笔记
    1978 Fibonacci数列 3
    1076 排序
    1205 单词翻转
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11186721.html
Copyright © 2020-2023  润新知