简体   繁体   中英

AttributeError: 'Customer' object has no attribute 'json' fastapi

i have model called customer has following fields

class Customer(Base):
    __tablename__ = "customer"
    id = Column(Integer, primary_key=True,index=True)
    name = Column(String(256))
    mobile = Column(String(256))

this is my query

def get_referal_details_lms(db1: Session, mobile:str):
    data= db1.query(models.Customer).filter(models.Customer.mobile == mobile).first()
    print (data)     ## <db.models.Customer object at 0x7f6902d8e340>
    print (data.json())    ##AttributeError: 'Customer' object has no attribute 'json'
    return data

What is issue here and to convert json in same model i have User model and for that it is working fine the on;y difference is User from one table and Customer from other table.i have two databases sessions and its not problem with db's.

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    email = Column(String(256))
    mobile = Column(String(256))

query for this model

def get_user(db: Session, id: int):
    data=db.query(models.User).filter(models.User.id == id).first()
    print(data)    ## <db.models.User object at 0x7f6902d8e340>
    print(data.json())  ##{'mobile': '11111111', 'id': 1, 'email': None}

Thanks for correcting me.

SQLAlchemy models in general have no json method. That must be something defined in your code somewhere, that's not defined for the Customer class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM