我有一个基本模型,该模型被其他两个模型继承。如何在保持ProcurementRow关系不变的情况下将Tender对象更改为OfferRequest对象?我的目的是根据需要将投标转换为OfferRequest。
class Procurement(models.Model):
pass
class ProcurementRow(models.Model)
procurement = models.ForeignKey(Procurement)
class Tender(Procurement):
pass
class OfferRequest(Procurement):
pass
答案 0 :(得分:1)
您可以遍历ClassCastException
的字段,然后使用getattr / setattr复制这些字段。我还建议通过两者之间的Procurement
建立牢固的关系,以便您可以跟踪事情的发生情况。
ForeignKeyField
答案 1 :(得分:0)
我已经通过原始sql查询解决了我的问题。
def convert_procurement(request, procurement_id):
procurement = get_object_or_404(Procurement, id=procurement_id).parent()
from django.db import connection
with connection.cursor() as cursor:
if isinstance(procurement, Tender):
cursor.execute("INSERT INTO procurement_offerrequest(procurement_ptr_id) VALUES (%s)", [procurement.id])
cursor.execute("DELETE FROM procurement_tender WHERE procurement_ptr_id=(%s)", [procurement.id])
return redirect('procurement:create_offer_request', procurement.id)
elif isinstance(procurement, OfferRequest):
cursor.execute("INSERT INTO procurement_tender(procurement_ptr_id) VALUES (%s)", [procurement.id])
cursor.execute("DELETE FROM procurement_offerrequest WHERE procurement_ptr_id=(%s)", [procurement.id])
return redirect('procurement:create_tender', procurement.id)