Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto copy object from model A to model B

I have this model classes:

class Article(models.Model):
    [many fields]

class ArticleArchive(models.Model):
    [same fields as Artilce model]

and I want to get objects from Article table and move it to ArticleArchive table:

articles = Article.objects.filter(date__year=2011)
for art in articles:
    [and there moving objects]

How to do it?

like image 640
Nips Avatar asked May 22 '12 07:05

Nips


1 Answers

articles = Article.objects.filter(date__year=2011).values()
for art in articles:
    ArticleArchive.objects.create(**art)
like image 198
Ignacio Vazquez-Abrams Avatar answered Sep 18 '22 13:09

Ignacio Vazquez-Abrams