可以使用Django Orm在外键字段上加入两个表吗?

发布于 2025-02-09 14:35:49 字数 1551 浏览 1 评论 0原文

class weapons(models.Model):
    weapon = models.CharField(max_length=11)
    country = models.IntegerField()
    flags = models.IntegerField(default=0)
    title = models.CharField(max_length=1000)

class compare(models.Model):
    weapon = models.CharField(max_length=11)
    user_id = models.IntegerField()
    flags = models.IntegerField(default=0)
    WeaponNode = models.ForeignKey(weapons, on_delete=models.PROTECT)

当我运行此功能时:

compare.objects.filter(user_id=1).values_list('weapon', 'WeaponNode__title')

我希望此查询raw:

SELECT apps_compare.weapon, apps_weapons.title FROM apps_compare INNER JOIN apps_weapons ON (apps_compare.weapon = apps_weapons.weapon) WHERE apps_compare.user_id = 1

结果应为:

”在此处输入映像说明“

但它返回了此:

SELECT "apps_compare"."weapon", "apps_weapons"."title" FROM "apps_compare" INNER JOIN "apps_weapons" ON ("apps_compare"."WeaponNode_id" = "apps_weapons"."id") WHERE "apps_compare"."user_id" = 1

apps_weapons.title return nulls null:

“

在我看到的其他示例中,我看到了,我看到了,他们只使用ID与Join On使用,但我想使用武器值而不是ID。我该如何完成ORM?如果ORM不可能,那么其​​他方法是什么?

class weapons(models.Model):
    weapon = models.CharField(max_length=11)
    country = models.IntegerField()
    flags = models.IntegerField(default=0)
    title = models.CharField(max_length=1000)

class compare(models.Model):
    weapon = models.CharField(max_length=11)
    user_id = models.IntegerField()
    flags = models.IntegerField(default=0)
    WeaponNode = models.ForeignKey(weapons, on_delete=models.PROTECT)

When I run this function:

compare.objects.filter(user_id=1).values_list('weapon', 'WeaponNode__title')

I expect this query raw:

SELECT apps_compare.weapon, apps_weapons.title FROM apps_compare INNER JOIN apps_weapons ON (apps_compare.weapon = apps_weapons.weapon) WHERE apps_compare.user_id = 1

Result should be:

enter image description here

But it returns this instead:

SELECT "apps_compare"."weapon", "apps_weapons"."title" FROM "apps_compare" INNER JOIN "apps_weapons" ON ("apps_compare"."WeaponNode_id" = "apps_weapons"."id") WHERE "apps_compare"."user_id" = 1

apps_weapons.title returns null:

enter image description here

In other examples I saw, they only used id with JOIN ON but I want to use a weapon value instead of id. How can I do accomplish with ORM? If it's not possible with ORM, then what are the other ways?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执妄 2025-02-16 14:35:49

您在这里使用foreferkey错误的方式。如果武器在您的中比较模型是指 武器模型,则应该是唯一的,并且然后将其用作 to_field =…   [django-doc]

class Weapon(models.Model):
    weapon = models.CharField(max_length=11, unique=True)
    # …

class Compare(models.Model):
    weapon = models.ForeignKey(
        Weapon,
        on_delete=models.CASCADE
        to_field='weapon',
        db_column='weapon'
    )
    # …

然后您可以查询:

Compare.objects.values('weapon', 'weapon__title')

You are here using the ForeignKey the wrong way. If the weapon in your Compare model refers to weapons in the Weapon model, it should be unique, and then use that as a to_field=… [Django-doc]:

class Weapon(models.Model):
    weapon = models.CharField(max_length=11, unique=True)
    # …

class Compare(models.Model):
    weapon = models.ForeignKey(
        Weapon,
        on_delete=models.CASCADE
        to_field='weapon',
        db_column='weapon'
    )
    # …

Then you can query with:

Compare.objects.values('weapon', 'weapon__title')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文