可以仅使用实例 ID 添加到 Django ManyToManyField
possible to add to Django ManyToManyField with only instance IDs
当您只有 ID 列表而没有数据库命中来检索实例时,是否可以添加到 ManyToManyField(add_instances_but_with_db_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)
是的,您可以只传递 ID:
self.workercombinedpayments.add(*id_list)
Facebook Django Python Web 框架小组在 'set' 上回答了我在 thread. Here are the Django docs 中的问题。注意,这将替换现有内容。如果您想更新您的字段而不是覆盖它,请参阅@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)
当您只有 ID 列表而没有数据库命中来检索实例时,是否可以添加到 ManyToManyField(add_instances_but_with_db_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)
是的,您可以只传递 ID:
self.workercombinedpayments.add(*id_list)
Facebook Django Python Web 框架小组在 'set' 上回答了我在 thread. Here are the Django docs 中的问题。注意,这将替换现有内容。如果您想更新您的字段而不是覆盖它,请参阅@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)