31 lines
1.2 KiB
Python
31 lines
1.2 KiB
Python
Sure, here's the `user.py` file for the `app/api/v1/models/` directory of the `blog_app` FastAPI backend project using SQLAlchemy with SQLite:
|
|
|
|
|
|
from sqlalchemy import Column, Integer, String, Boolean
|
|
from app.db import Base
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
username = Column(String, unique=True, index=True)
|
|
email = Column(String, unique=True, index=True)
|
|
hashed_password = Column(String)
|
|
is_active = Column(Boolean, default=True)
|
|
is_superuser = Column(Boolean, default=False)
|
|
|
|
def __repr__(self):
|
|
return f"User(id={self.id}, username='{self.username}', email='{self.email}', is_active={self.is_active}, is_superuser={self.is_superuser})"
|
|
|
|
Explanation:
|
|
|
|
|
|
|
|
|
|
4. We define the following columns for the `User` model:
|
|
- `id`: an integer primary key with an index
|
|
- `username`: a string column that is unique and indexed
|
|
- `email`: a string column that is unique and indexed
|
|
- `hashed_password`: a string column to store the hashed password
|
|
- `is_active`: a boolean column to indicate if the user is active or not (default is `True`)
|
|
- `is_superuser`: a boolean column to indicate if the user is a superuser or not (default is `False`) |