Last active
June 21, 2021 20:17
-
-
Save mpcabd/c7bd45126babe4650902 to your computer and use it in GitHub Desktop.
Archived models in Django 1.4.x
This file contains hidden or 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
from django.db import models | |
class MyModelBase(models.Model): | |
class Meta: | |
abstract = True | |
field1 = models.CharField(max_length=256) | |
field2 = models.BooleanField(db_index=True, default=True) | |
#.... | |
class MyModel(MyModelBase): | |
@staticmethod | |
def create_from_archived(object): | |
return MyModel.objects.create(id=object.my_model_id, **dict([ | |
(fild.name, getattr(object, fild.name)) | |
for fild in object._meta.fields | |
if fild.name not in ['id', 'my_model_id'] | |
])) | |
class ArchivedMyModel(MyModelBase): | |
my_model_id = models.IntegerField(db_index=True) | |
@staticmethod | |
def create_from_instance(object): | |
return ArchivedMyModel.objects.create(my_model_id = object.id, **dict([ | |
(fild.name, getattr(object, fild.name)) | |
for fild in object._meta.fields | |
if fild.name != 'id' | |
])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment