Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulating Models from other Models in Django?
    primarykey
    data
    text
    <p>This is somewhat related to the question posed in <a href="https://stackoverflow.com/questions/1398857/django-model-inheritance-can-i-change-model-type">this question</a> but I'm trying to do this with an abstract base class.</p> <p>For the purposes of this example lets use these models:</p> <pre><code>class Comic(models.Model): name = models.CharField(max_length=20) desc = models.CharField(max_length=100) volume = models.IntegerField() ... &lt;50 other things that make up a Comic&gt; class Meta: abstract = True class InkedComic(Comic): lines = models.IntegerField() class ColoredComic(Comic): colored = models.BooleanField(default=False) </code></pre> <p>In the view lets say we get a reference to an <code>InkedComic id</code> since the tracer, err I mean, inker is done drawing the lines and it's time to add color. Once the view has added all the color we want to save a <code>ColoredComic</code> to the db.</p> <p>Obviously we could do</p> <pre><code>inked = InkedComic.object.get(pk=ink_id) colored = ColoredComic() colored.name = inked.name etc, etc. </code></pre> <p>But really it'd be nice to do:</p> <pre><code>colored = ColoredComic(inked_comic=inked) colored.colored = True colored.save() </code></pre> <p>I tried to do</p> <pre><code>class ColoredComic(Comic): colored = models.BooleanField(default=False) def __init__(self, inked_comic = False, *args, **kwargs): super(ColoredComic, self).__init__(*args, **kwargs) if inked_comic: self.__dict__.update(inked_comic.__dict__) self.__dict__.update({'id': None}) # Remove pk field value </code></pre> <p>but it turns out the <code>ColoredComic.objects.get(pk=1)</code> call sticks the <code>pk</code> into the <code>inked_comic</code> keyword, which is obviously not intended. (and actually results in a <code>int</code> does not have a <code><strong>dict</strong></code> exception)</p> <p>My brain is fried at this point, am I missing something obvious, or is there a better way to do this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload