Внедрение Python Elixir OneToMany и ManyToOne: Вкл Вставить новую запись с отношениями OneToMany? - PullRequest
0 голосов
/ 04 декабря 2011

Как вставить запись с отношениями один-ко-многим в 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()   

1 Ответ

0 голосов
/ 04 декабря 2011

только что разобрался.Его ...

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