当您只有ID列表时,可以添加到ManyTomanyField,而没有DB命中以检索实例(add_instances_but_with_db_hit可以做到这一点,但是hit hit hit hit hit the hit(。
谢谢。
class WorkerCombinedPayment(AbstractReimbursement):
charge_id = models.TextField(blank=True, null=True)
paid = models.BooleanField(default=False)
worker = models.ForeignKey(Worker)
class MassPayment(TimeStampedModel):
list_payments = JSONField(blank=True, null=True)
paypal_batch_id = models.TextField(blank=True, null=True)
success = models.NullBooleanField()
response_from_paypal = JSONField(blank=True, null=True)
workercombinedpayments = models.ManyToManyField(WorkerCombinedPayment)
def add_instances_but_with_db_hit(self, id_list):
found = WorkerCombinedPayment.objects.filter(id__in=id_list)
self.workercombinedpayments.add(found)
是的,您可以通过IDS:
self.workercombinedpayments.add(*id_list)
facebook django python Web框架组在此主题中回答了我的问题。这是"集合"上的Django文档。NB这将替换现有内容。如果您想更新领域,而不是覆盖它,请参见 @danielroseman的接受答案。
class WorkerCombinedPayment(AbstractReimbursement):
charge_id = models.TextField(blank=True, null=True)
paid = models.BooleanField(default=False)
worker = models.ForeignKey(Worker)
class MassPayment(TimeStampedModel):
list_payments = JSONField(blank=True, null=True)
paypal_batch_id = models.TextField(blank=True, null=True)
success = models.NullBooleanField()
response_from_paypal = JSONField(blank=True, null=True)
workercombinedpayments = models.ManyToManyField(WorkerCombinedPayment)
def add_instances(self, id_list):
self.workercombinedpayments.set(id_list)