Skip to content

Instantly share code, notes, and snippets.

@ychennay
Created March 22, 2020 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ychennay/8771f582219252f79c381bec38a9a0f6 to your computer and use it in GitHub Desktop.
Save ychennay/8771f582219252f79c381bec38a9a0f6 to your computer and use it in GitHub Desktop.
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