Hey I have following house schema:
class HouseCreate(HouseBase):
title : str
rent : int
no_rooms : int
size : int
additional_cost : int
total_rent : int
max_flatmates : int
exist_fmates : int
heating_type : str
heating_cost : int
city : str
adress : str
zipcode : int
description : str
min_duration : int
smoking_allowed : bool
parking : str
postedby : str
is_active : bool
date_posted : Optional[date] = datetime.now().date()
and Db-model
class House(Base):
id = Column(Integer,primary_key = True, index=True)
title = Column(String,nullable= False)
rent = Column(Integer,nullable = False)
no_rooms = Column(Integer,nullable = False)
size = Column(Integer,nullable = False)
additional_cost = Column(Integer,nullable = False)
total_rent = Column(Integer,nullable = False)
max_flatmates = Column(Integer,nullable = False)
exist_fmates = Column(Integer,nullable = False)
heating_type = Column(String,nullable= False)
heating_cost = Column(Integer,nullable = False)
city = Column(String,nullable = False)
adress = Column(String,nullable = False)
zipcode = Column(Integer,nullable = False)
description = Column(String,nullable=False)
min_duration = Column(Integer,nullable = False)
smoking_allowed = Column(Boolean,nullable = False)
parking = Column(String,nullable = False)
postedby = Column(String,nullable = False)
date_posted = Column(Date)
is_active = Column(Boolean(),default=True)
owner_id = Column(Integer,ForeignKey("user.id"))
owner = relationship("User",back_populates="houses")
So, I wanted to add also upload file() option too. I tried many ways but didn't work well. Also, I wasn't sure about the connection to database. So, the things are getting messy here. I followed one of the post here but still not getting it. How can I implement the upload files option to the schemas and save it to the database. Basically I just want to let user to create a house with photos and some informations above.
Below that you can see the last method I have tried. But failure.
@router.put("/create-house/{house_id}", response_model=House)
async def update_house(request: Request, house_id:int,db: Session = Depends(get_db),
additional_cost : int = Form(None),
title: str = Form(None),
rent : int = Form(None),
no_rooms : int = Form(None),
size : int = Form(None),
total_rent : int = Form(None),
max_flatmates : int = Form(None),
exist_fmates : int = Form(None),
heating_type : str = Form(None),
heating_cost : int = Form(None),
city : str = Form(None),
adress : str = Form(None),
zipcode : int = Form(None),
description : str =Form(None),
min_duration : int = Form(None),
smoking_allowed : bool = Form(None),
parking : str = Form(None),
postedby : str = Form(None),
date_posted : Optional[date] = datetime.now().date(),
is_active: str = Form(None),
is_superuser: bool = Form(None),
is_deleted: bool = Form(None),
profile_pc: UploadFile = File(None)):
house = HouseUpdate(title = title,
rent = rent,
no_rooms = no_rooms,
size = size,
additional_cost = additional_cost,
total_rent = total_rent,
max_flatmates = max_flatmates,
exist_fmates = exist_fmates,
heating_type = heating_type,
heating_cost = heating_cost,
city = city,
adress = adress,
zipcode = zipcode,
description = description,
min_duration = min_duration,
smoking_allowed = smoking_allowed,
parking = parking,
postedby = postedby,
is_active = is_active,
date_posted = date_posted,
is_deleted=is_deleted)