Reputation: 1649
I have a queryset and I want to insert the results of that queryset into a database table (say SelectedFeatures
).
all_features = Feature.objects.filter(product_id = selected_product_id)
The structure of the new table and the Feature
table would almost be the same except one field. Should I just insert all the fields of the queryset manually like inserting records into a table or is there another way to cater this?
Upvotes: 0
Views: 508
Reputation: 6733
I like @doniyor's solution but if you need more control (for example if field names aren't exactly the same) you could iterate over the fields and possibly make some decisions:
for feature in Feature.objects.filter(product_id = selected_product_id).all():
selected_feature = SelectedFeature()
for attr in SelectedFeature._meta.get_all_field_names():
selected_feature.setattr(attr, feature.getattr(attr))
selected_feature.save()
Upvotes: 1