Refresh latest data from db. When this method is called without arguments all db fields of the model are updated to the values currently present in the database. .. code-block:: python3 user.refresh_from_db(fields=['name']) :param fields: The special f
(
self,
fields: Iterable[str] | None = None,
using_db: BaseDBAsyncClient | None = None,
)
| 1223 | await db.executor_class(model=self.__class__, db=db).fetch_for_list([self], *args) |
| 1224 | |
| 1225 | async def refresh_from_db( |
| 1226 | self, |
| 1227 | fields: Iterable[str] | None = None, |
| 1228 | using_db: BaseDBAsyncClient | None = None, |
| 1229 | ) -> None: |
| 1230 | """ |
| 1231 | Refresh latest data from db. When this method is called without arguments |
| 1232 | all db fields of the model are updated to the values currently present in the database. |
| 1233 | |
| 1234 | .. code-block:: python3 |
| 1235 | |
| 1236 | user.refresh_from_db(fields=['name']) |
| 1237 | |
| 1238 | :param fields: The special fields that to be refreshed. |
| 1239 | :param using_db: Specific DB connection to use instead of default bound. |
| 1240 | |
| 1241 | :raises OperationalError: If object has never been persisted. |
| 1242 | """ |
| 1243 | if not self._saved_in_db: |
| 1244 | raise OperationalError("Can't refresh unpersisted record") |
| 1245 | db = using_db or self._choose_db() |
| 1246 | qs = QuerySet(self.__class__).using_db(db) |
| 1247 | if fields: |
| 1248 | qs = qs.only(*fields) |
| 1249 | obj = await qs.get(pk=self.pk) |
| 1250 | |
| 1251 | for field in fields or self._meta.db_fields: |
| 1252 | setattr(self, field, getattr(obj, field, None)) |
| 1253 | |
| 1254 | @classmethod |
| 1255 | def _choose_db(cls, for_write: bool = False) -> BaseDBAsyncClient: |