Insert into select query support in Django

Is there any way in Django ORM to execute insert into select postgres statement.

# Django models
class FileSet(models.Model):
    use_case = models.ForeignKey("UseCase", related_name="file_sets")

class TrainingFileSet(Base):
    file_set = models.ForeignKey(FileSet, on_delete=models.PROTECT)

I want to write the below query using Django ORM and wondering if it’s possible to do so

INSERT INTO TrainingFileSet (file_set_id)
SELECT id
FROM FileSet
WHERE use_case_id=1;
1 Like

I am not aware of any - at least not one that directly executes as one query.

I’d probably do this using bulk_create with the objs parameter created from a queryset on the FileSet model.

Or, if you’re talking about a truly large amount of data ( > 1,000,000 rows), then I’d think about doing it as a raw SQL statement.

Thanks @KenWhitesell
Yes, I’ve been using the raw SQL only as of now since an year. Just posting here to understand if there was any new update with this feature or any coming updates might contain this one.