[Django]-Populating Models from other Models in Django?

3👍

What about a static method on the class to handle this?

colored = ColoredComic.create_from_Inked(pk=ink_id)
colored.colored = True
colored.save()

Untested, but something to this effect (using your code from above)

class ColoredComic(Comic):
    colored = models.BooleanField(default=False)

    @staticmethod
    def create_from_Inked(**kwargs):
        inked = InkedComic.objects.get(**kwargs)
        if inked:
            colored = ColoredComic.objects.create()
            colored.__dict__.update(inked.__dict__)
            colored.__dict__.update({'id': None}) # Remove pk field value
            return colored
        else:
            # or throw an exception...
            return None

0👍

For that simple case, this will work:

inked = InkedComic.object.get(pk=ink_id)
inked.__class__ = ColoredComic
inked.colored = True
inked.save()

Leave a comment