class MyThread2(threading.Thread):

    def run(self):
        for q in range(1, 100):
            for i in range(1, 5):
                pub = Publication.objects.all()[random.randint(1, 2)]
for j in range(1, 5):
    article = Article.objects.all()[random.randint(1, 15)]
ap, c = ArticlePublication.objects.get_or_create(article=article,
    publication=pub)
print('Get  or create', self.name)
Django: IntegrityError during Many To Many add()