Created
May 2, 2015 14:49
-
-
Save rg3915/9c2c0d6b9db0a5e6e6e1 to your computer and use it in GitHub Desktop.
Copiar registros de tabela OneToMany
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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