gpt4 book ai didi

Django 。如何获取GenericForeignKey对应的对象

转载 作者:行者123 更新时间:2023-12-04 01:38:39 25 4
gpt4 key购买 nike

简单的问题。我有这个模型:

class Discount(models.Model):
discount_target = GenericForeignKey('content_type', 'object_id')
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, blank=True, null=True)
object_id = models.PositiveIntegerField(blank=True, null=True)

如何获取对象 GenericForeignKey是指?

最佳答案

您只需像访问任何其他字段一样访问它。

my_discount_obj.discount_target

关于 Django 。如何获取GenericForeignKey对应的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48418562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com