Skip to content

Instantly share code, notes, and snippets.

@rg3915
Created May 2, 2015 14:49
Show Gist options
  • Save rg3915/9c2c0d6b9db0a5e6e6e1 to your computer and use it in GitHub Desktop.
Save rg3915/9c2c0d6b9db0a5e6e6e1 to your computer and use it in GitHub Desktop.
Copiar registros de tabela OneToMany
from vendas_project.vendas.models import Sale, SaleDetail
s = Sale.objects.filter(pk=1) # filtra a Venda pelo pk
d = SaleDetail.objects.filter(sale=s) # filtra os itens dessa Venda
s = Sale.objects.get(pk=s) # com o get pega o pk da Venda que foi filtrada
s.pk = None
s.save() # salva uma cópia da Venda
for i in d:
n = SaleDetail.objects.create(
sale=s, product=i.product, quantity=i.quantity, price_sale=i.price_sale, subtotal=i.quantity * i.price_sale)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment