Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Elixir OneToMany和ManytOne实现:关于插入具有OneToMany关系的新记录?_Python_Sqlalchemy_Cpython_Python Elixir - Fatal编程技术网

Python Elixir OneToMany和ManytOne实现:关于插入具有OneToMany关系的新记录?

Python Elixir OneToMany和ManytOne实现:关于插入具有OneToMany关系的新记录?,python,sqlalchemy,cpython,python-elixir,Python,Sqlalchemy,Cpython,Python Elixir,如何在Python Elixir中插入具有一对多关系的记录?请参阅下面的代码 from elixir import * class Product(Entity): using_options(shortnames=True) name = Field(Unicode) category = ManyToOne('Category') brand = ManyToOne('Brand') barcode = Field(String) c

如何在Python Elixir中插入具有一对多关系的记录?请参阅下面的代码

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()   

我刚想出来。它

asrock = Brand(name='Asrock')
asus = Brand(name='Asus')
mobo = Category(name='Motherboard')
proc = Category(name='Processor')     
session.commit()

p1 = Product(name='N98', cost=2100.50, price=2500.50, order_quantity=5, unit='unit',    
    category=mobo, brand=asrock)

p2 = Product(name='M-N98', cost=2300.50, price=2500.50, order_quantity=5, unit='unit',    
    category=mobo, brand=asus)

session.commit()