python - SQLAlchemy one to many relationship, how to filter the collection -


बौद्धिक रूप से उत्पाद को उत्पादकचित्र से कई रिश्ते हैं। < / p>

मेरे उत्पाद चित्र मॉडल इस तरह दिखता है:

  picture_type_enums = ( 'मुख्य', 'संबंधित', 'विकल्प') वर्ग ProductPicture (आधार): __tablename__ = 'product_pictures' picture_id = कॉलम (पूर्णांक, primary_key = सच) product_id = कॉलम (पूर्णांक, ForeignKey ( 'products.product_id')) picture_type = कॉलम (Enum (* picture_type_enums)) url = कॉलम (स्ट्रिंग (120))   

और मेरा उत्पाद मॉडल इस तरह दिखता है:

  वर्ग उत्पाद (बेस): __tablename__ = 'उत्पाद' product_id = स्तंभ (पूर्णांक, प्राथमिक_की = सच) product_name = स्तंभ (स्ट्रिंग ( 100)) product_pictures = संबंध ("उत्पाद की तस्वीर")   

मेरा प्रश्न एक उत्पाद के लिए है, मेरे पास विभिन्न प्रकार के उत्पाद चित्र हैं मुझे पता है कि मेरे पास एक उत्पाद उदाहरण पी है, मैं सभी प्रकार के उत्पाद चित्रों को प्राप्त करने के लिए p.product_pictures कॉल कर सकता हूं। हालांकि, मुझे p.main_pictures की तरह कुछ चाहिए जो सभी प्रकार के चित्रों को प्राप्त करें 'main' , और p.option_pictures सभी को मिलता है < Code> product_pictures प्रकार का 'विकल्प' । ऐसा करने का एक अच्छा तरीका है।

धन्यवाद

अगर आप इसके लिए प्रलेखन, आप देख सकते हैं कि आप primaryjoin तर्क का उपयोग करके स्पष्ट रूप से स्थिति को परिभाषित करके रिश्ते को और सीमित कर सकते हैं, उदाहरण के साथ जो आपके आवश्यक परिदृश्य को स्पष्ट रूप से दिखा रहा है अपनी आवश्यकताओं के अनुसार, अब उत्पाद वर्ग निम्न प्रकार है:

  वर्ग उत्पाद (बेस): __tablename__ = 'products' product_id = स्तंभ (पूर्णांक, प्राथमिक_की = सच) product_name = कॉलम (स्ट्रिंग (100)) product_pictures = संबंध ( "ProductPicture") main_pictures = संबंध ( "ProductPicture", primaryjoin = "and_ (Product.product_id == ProductPicture.product_id," "ProductPicture.picture_type == 'मुख्य') ") option_pictures = संबंध (" ProductPicture ", primaryjoin =" and_ (Product.product_id == ProductPicture.product_id, "" ProductPicture.picture_type == 'विकल्प') ")   

उदाहरण सत्र :

  & gt; & gt; & gt; पी = उत्पाद () & gt; & gt; & gt; P.product_name = 'परीक्षण उत्पाद' & gt; & gt; & gt; P.product_id = 1> & gt; & gt; & gt; Session.add (p) & gt; & gt; & gt; Pic1 = उत्पादचित्र ()> & gt; & gt; & gt; Pic1.product_id = p.product_id & gt; & gt; & gt; Pic1.picture_type = 'मुख्य' & gt; & gt; & gt; Pic1.url = 'http://example.com/p1.main.png' & gt; & gt; & gt; Session.add (pic1) & gt; & gt; & gt; Pic2 = उत्पादचित्र ()>> gt; & gt; & gt; Pic2.product_id = p.product_id & gt; & gt; & gt; Pic2.picture_type = 'विकल्प' & gt; & gt; & gt; Pic2.url = 'http://example.com/p1.option1.png' & gt; & gt; & gt; Session.add (pic2) & gt; & gt; & gt; Pic3 = उत्पादचित्र ()>> gt; & gt; & gt; Pic3.product_id = p.product_id & gt; & gt; & gt; Pic3.picture_type = 'विकल्प' & gt; & gt; & gt; Pic3.url = 'http://example.com/p1.option2.png' & gt; & gt; & gt; Session.add (pic3) & gt; & gt; & gt; Session.commit () & gt; & gt; & gt; [(Pic.picture_type, pic.url) p.product_pictures में पिक के लिए] [(u'main ', u'http: //example.com/p1.main.png'), (u'option ', यू' Http://example.com/p1.option1.png '), (u'option', u'http: //example.com/p1.option2.png ')] & gt; & gt; & gt; [(Pic.picture_type, pic.url) p.main_pictures में pic के लिए] [(u'main ', u'http: //example.com/p1.main.png')] & gt; & gt; & gt; [(Pic.picture_type, pic.url) p.option_pictures में पिक के लिए] [(u'option ', u'http: //example.com/p1.option1.png'), (u'option ', यू' Http://example.com/p1.option2.png ')]    

Comments

Popular posts from this blog

php - how to change mysql_result($res, 0, "url"); to mysqli -

Delphi: missing interface declarations for UI Automation methods -

android - How to attach an image from an app to gmail -