【问题标题】:Trouble accessing backref parameters in HTML using SQLAlchemy and Flask使用 SQLAlchemy 和 Flask 在 HTML 中访问 backref 参数时遇到问题
【发布时间】:2020-08-25 18:57:46
【问题描述】:

我有一个 HTML 页面(如下所示),我正在尝试使用 backref 访问一些表格数据。 HTML {% for buyer in company.buyer%} {% if buyer is none %} 引用 Company 表的 backref buyer 以使我可以访问 Buyers 表。

从那里,我想使用 Buyer 表(edetails)中的 backref 从 Eventdetails 表中获取信息。

当我尝试访问 Eventdetails 表的信息时,这不起作用。 {{ buyer.edetails.naics}} 没有信息填充。是否可以同时使用两个不同的反向引用?谢谢!

company.html |简化以便于阅读

          <div class="container">
            {% for buyer in company.buyer%} {% if buyer is none %}
            <div class="row">
              <div class="col-sm">
                No company has been added.
              </div>
            </div>
            {% else %}
            <!--This is where I start the internal table-->
            <div class="container border bg-light">
              <div class="row">
                <div class="col-sm">
                  {{buyer.firstname}} {{buyer.lastname}}
                </div>
                <div class="col-sm">
                  <!--THIS CODE DOESN'T POPULATE ANY INFORMATION-->
                  <p> {{ buyer.edetails.naics}}</p>
                </div>
              </div>
            </div>
            {% endif %} {% endfor %}
          </div>

views.py

class CompanyView(MethodView):

    decorators = [login_required]
    template_file = 'company.html'

    def get(self, comp_id, event_id):
        company = Company.query.filter_by(id=comp_id).first()
        print(f'The company is {company.company}')
        events = Events.query.filter_by(id=event_id).first()
        print(f'The event is {events.eventname}')
        return render_template(self.template_file, company=company, events=events)

此处列出的型号

models.py

Class Company(db.Model, UserMixin):

    __tablename__ = 'company'

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(64), unique=True, index=True, nullable=False)
    company = db.Column(db.String(64), index=True, nullable=False)
    company_url = db.Column(db.String(64), index=True, nullable=False)
    duns = db.Column(db.String(11))
    event_id = db.relationship(
        'Events', secondary=event_company, backref='event', lazy='dynamic'
    )
    password_hash = db.Column(db.String(128))
    # Company is parent to the buyer
    buyer = db.relationship('Buyers', backref='buyer', lazy='dynamic')


class Buyers(db.Model, UserMixin):
    __tablename__ = 'buyers'

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(64), unique=True, index=True, nullable=False)
    firstname = db.Column(db.String(64), index=True, nullable=False)
    lastname = db.Column(db.String(64), index=True, nullable=False)
    company = db.Column(db.Integer, db.ForeignKey('company.id'))
    edetails = db.relationship('Eventdetails', backref='edetails', lazy='dynamic')
    schedule = db.relationship('Buyerschedule', backref='schedule', lazy='dynamic')

class Eventdetails(db.Model):

    __tablename__ = 'eventdetails'
    id = db.Column(db.Integer, primary_key=True)
    schedule_name = db.Column(db.String(64), unique=True, index=True)
    events_id = db.Column(db.Integer, db.ForeignKey('events.id'))
    buyer_id = db.Column(db.Integer, db.ForeignKey('buyers.id'))
    naics = db.Column(db.Integer)
    buyer_schedule_id = db.relationship(
        'Buyerschedule', backref='buyer_sched_id', lazy='dynamic')

【问题讨论】:

    标签: python-3.x flask sqlalchemy flask-sqlalchemy


    【解决方案1】:

    如果您对buyer.edetails.naics 感兴趣,则无需使用反向引用。这只是买方与 Eventdetails 的关系。

    按照现在的设置方式,buyer.edetails 将返回一个列表。所以如果你尝试访问buyer.edetails.naics,你可能会得到一个错误

    如果买家和 eventdetails 之间确实存在一对一的关系,那么您可以这样配置关系:

    class Buyers(db.Model, UserMixin):
    ...
        edetails = db.relationship('Eventdetails', backref='edetails', lazy='dynamic', uselist=False)
    

    您可能还必须指定 eventdetails.buyer_id 列是唯一的。

    【讨论】:

    • 这是有道理的。感谢您在我的“循环”之外帮助我。我忘记了这将是一个列表。谢谢!
    猜你喜欢
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多