Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8504065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:55:28+00:00 2026-06-11T01:55:28+00:00

I need to write two classes like this: class Item(Base, DBBase): __tablename__ = ‘items’

  • 0

I need to write two classes like this:

class Item(Base, DBBase):
    __tablename__ = 'items'

    id = Column(Integer, primary_key = True)
    name = Column(String)
    description = Column(String)
    price = Column(Float, default = 0)
    on_sell = Column(Boolean, default = False)

    img = Column(String)

    attributes = relationship('ItemAttribute')

    def __init__(self, name, description):
        self.name = name
        self.description = description

class ItemAttribute(Base, DBBase):
    __tablename__ = 'itemattributes'

    id = Column(Integer, primary_key = True)
    name = Column(String, nullable = False)
    value = Column(String, nullable = False)

    item_id = Column(Integer, ForeignKey('items.id'))
    item = relationship('Item')

    def __init__(self, name, value):
        self.name = name
        self.value = value

One item can own several attributes, and I need to:
1. insert some methods on class Item to easily do CURD(insertion, deletion, update and query) attributes for it. I need to search a attribute of a item and return it’s corresponding value.
2. have the ability to search items by attributes. For example, some items have the attributes of ‘Feature’ = ‘True’. I need to get all items which have this attribute.

Thanks for help. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:55:29+00:00Added an answer on June 11, 2026 at 1:55 am

    If you add backref onto your ItemAttribute relationship:

    item_id = Column(Integer, ForeignKey('items.id', onupdate='CASCADE', ondelete='CASCADE'))
    item = relationship(Items, backref='attributes')
    

    This will create and Item.attributes[] array which contains the ItemAttribute’s. You might also add the onupdate and ondelete if you’re using mysql.

    Then when you query, you can do this:

    rs = mySession.query(Items)
    firstItem = rs.first()
    for attribute in firstItem.attributes:
       print attribute
    

    When querying you can filter by joining the backref:

    rs = mySession.query(Items).join(Items.attributes).filter(ItemAttribute.name=='somethingSpecial')
    

    Further, if it’s a one to one relationship (but it’s not in this case), you could skip the list by specifing uselist=False:

    item = relationship(ITEM, backref='attribute', uselist=False)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes : base class name Component and inheritd class named DBComponent
Just like the title says I am having problems combining two classes. Altough this
Ok, I need a little help. I have these two PHP classes class menu
I need to write data into drive. I have two options: write raw sectors.(_write(handle,
I need to draw a graph of write accesses of two concurrently running threads.
I'm designing a class that has two dependencies. One of the dependency classes has
I would like to have a mapped class named Message. This class should include
I have two classes with a bi-directional @OneToOne mapping to each other. Class A
I need the correct syntax to write two loops inside each other. First one
I need to blink between two colors:red and white. I've found that after $targetcss('background-color','red')

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.