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 8513175
In Process

The Archive Base Latest Questions

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

I wrote two models like these: class ItemType(Base, DBBase): __tablename__ = ‘itemtypes’ id =

  • 0

I wrote two models like these:

class ItemType(Base, DBBase):
    __tablename__ = 'itemtypes'

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

    comments = relationship('ItemTypeComment')

class ItemTypeComment(Base, DBBase):
    __tablename__ = 'itemtypecomments'

    id = Column(Integer, primary_key = True)
    comment = Column(String), nullable = True)

    itemtype_id = Column(Integer, ForeignKey('itemtypes.id'), nullable = True)
    itemtype = relationship('ItemType')

Here we can see, one ItemType can have several ItemTypeComments. Now I wanna add a method to class ItemType to easily add a comment to it. Currently, i wrote:

def add_comment(self, comment):
    comment = ItemTypeComment()
    comment.comment = comment
    comment.itemtype = self
    DBSession.add(comment)

I wanna know if there is any better way doing this? Thanks.

  • 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-11T04:29:00+00:00Added an answer on June 11, 2026 at 4:29 am

    Well, there’s nothing wrong with the code you have. You can, however, simplify add_comment a little bit by using the .append() method:

    def add_comment(self, comment):
        comment = ItemTypeComment()
        self.comments.append(comment)
    

    This eliminates having to set comment.itemtype, as well as manually adding the new object to your DBSession.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to join two models by a column which
I wrote two methods in class Util: public static final <T> T[] copy1(T[] source)
I wrote two methods to check there performance public class Test1 { private String
I have two models: class Foo(models.Model): foo_field = ... class Bar(models.Model): foo = models.ForeignKey(Foo,
I have a simple one-to-many (models.ForeignKey) relationship between two of my model classes: class
I have two models with managers: Category and Project. class PopulatedCategoriesManager(models.Manager): def get_query_set(self): return
I wrote two small applications (a client and a server) to test UDP communication
I wrote two code both are same Using NSURLConnection and Using AFNetworking. My NSURLConnection
I wrote two small java programs: a TCP client which sends many lines of
I wrote two methods with a void type parameter: procedure Method1(const MyVar; size: cardinal);

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.