バックリファレンスを使用していくつかのテーブルデータにアクセスしようとしているHTMLページ(以下に簡略化)があります。HTMLは、Companyテーブルの{% for buyer in company.buyer%} {% if buyer is none %}
backrefbuyer
を参照して、Buyersテーブルにアクセスできるようにします。
そこから、Buyerテーブル(edetails)のbackrefを使用して、Eventdetailsテーブルから情報を取得したいと思います。
これが機能しないのは、Eventdetailsテーブルの情報にアクセスしようとしたときです。{{ buyer.edetails.naics}}
情報は入力されません。2つの異なるバックリファレンスを一緒に使用することは可能ですか?ありがとうございました!
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')
に興味がbuyer.edetails.naics
ある場合は、backrefを使用する必要はありません。これは、Eventdetailsに対するバイヤーの関係にすぎません。
現在の設定方法でbuyer.edetails
は、リストが返されます。したがって、アクセスしようとするとエラーが発生する可能性がありますbuyer.edetails.naics
バイヤーとeventdetailsの間に本当に1対1の関係がある場合は、次のように関係を構成できます。
class Buyers(db.Model, UserMixin):
...
edetails = db.relationship('Eventdetails', backref='edetails', lazy='dynamic', uselist=False)
eventdetails.buyer_id
列が一意であることを指定する必要がある場合もあります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加