Created
March 22, 2020 06:17
-
-
Save ychennay/8771f582219252f79c381bec38a9a0f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Model(metaclass=ModelBase): | |
# ... | |
@classmethod | |
def from_db(cls, db, field_names, values): | |
if len(values) != len(cls._meta.concrete_fields): | |
values_iter = iter(values) | |
values = [ | |
next(values_iter) if f.attname in field_names else DEFERRED | |
for f in cls._meta.concrete_fields | |
] # values is a tuple ('Yu', 'Chen', '123') | |
new = cls(*values) # new is a Student instance with attributes initialized like a regular Python instance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment