-
-
Save gollum23/a4f8d6f4abc432f24fe6 to your computer and use it in GitHub Desktop.
Copiar registros de tabela OneToMany
This file contains hidden or 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