39 lines
1.8 KiB
Python
39 lines
1.8 KiB
Python
```python
|
|
|
|
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})"
|
|
```
|
|
|
|
|
|
- `id`: An integer primary key and index.
|
|
- `username`: A string representing the user's username, which must be unique and indexed.
|
|
- `email`: A string representing the user's email address, which must be unique and indexed.
|
|
- `hashed_password`: A string representing the hashed password of the user.
|
|
- `is_active`: A boolean indicating whether the user is active or not, with a default value of `True`.
|
|
- `is_superuser`: A boolean indicating whether the user is a superuser or not, with a default value of `False`.
|
|
|
|
The `__repr__` method is defined to provide a string representation of the `User` object when printed.
|
|
|
|
Make sure to import the necessary modules and classes at the top of the file:
|
|
|
|
```python
|
|
from sqlalchemy import Column, Integer, String, Boolean
|
|
from app.db import Base
|
|
```
|
|
|
|
Here, `Column`, `Integer`, `String`, and `Boolean` are imported from the `sqlalchemy` module, and `Base` is imported from `app.db`, which is assumed to be the base class for SQLAlchemy models in your FastAPI application.
|
|
|
|
Note: This code assumes that you have already set up the necessary database connections and configurations for your FastAPI application using SQLAlchemy and SQLite. |