0
파이썬 엘릭서에서 일대 다 관계로 레코드를 어떻게 삽입합니까? 아래 코드를 참조하십시오.Python Elixir OneToMany 및 ManyToOne 구현 : On OneToMany 관계로 새 레코드 삽입?
from elixir import *
class Product(Entity):
using_options(shortnames=True)
name = Field(Unicode)
category = ManyToOne('Category')
brand = ManyToOne('Brand')
barcode = Field(String)
cost = Field(Float)
price = Field(Float)
order_quantity = Field(Float)
unit = Field(Unicode)
class Category(Entity):
using_options(shortnames=True)
name = Field(Unicode)
product = OneToMany('Product')
def main():
metadata.bind = 'sqlite:///pypos.sqlite'
metadata.bind.echo = False
setup_all()
create_all()
Brand(name='Asrock')
Brand(name='Asus')
Category(name='Motherboard')
Category(name='Processor')
session.commit()
p = Product(name='N98', cost=2100.50, price=2500.50, order_quantity=5, unit='unit')
'''
How do you add a Category and a Brand on the Product table?
Is there a lookup for this?
'''
#Is there an alternative to this two instruction?
p.category = Category(name='Motherboard')
p.brand = Brand(name='Asrock')
session.commit()
if__name__=='__main__': main()