gpt4 book ai didi

django - 模型妈妈 : Multiple recipes with foreign key relation to a single recipe

转载 作者:行者123 更新时间:2023-11-28 19:54:23 26 4
gpt4 key购买 nike

我对 ModelMommy 有一段时间的烦恼,但我不知道如何正确地做到这一点。

让我们假设一个简单的关系:

class Organization(models.Model):
label = models.CharField(unique=True)

class Asset(models.Model):
organization = models.ForeignKey(Organization)
label = models.CharField(unique=True)

和食谱:

from model_mommy.recipe import Recipe, foreign_key


organization_recipe = Recipe(Organization, label='My Organization')
asset1_recipe = Recipe(Asset,
organization=foreign_key(organization_recipe),
label='asset 1')
asset2_recipe = Recipe(Asset,
organization=foreign_key(organization_recipe),
label='asset 2')

现在当我制作这些 Assets 配方时出现错误:

>> asset1 = asset1_recipe.make()
>> asset2 = asset2_recipe.make()
IntegrityError: duplicate key value violates unique constraint "organizations_organization_label_key"
DETAIL: Key (label)=(My Organization) already exists.

这可以通过将 asset1 的组织作为参数提供给 asset2 的 make 方法来解决:

>> asset1 = asset1_recipe.make()
>> asset2 = asset2_recipe.make(organization=asset1.organization)

但必须有一种更简单、更干净的方法来做到这一点。

编辑

根据 Helgi 的回答中的链接,我更改了所有食谱外键以指向闭包:

def organization_get_or_create(**kwargs):
"""
Returns a closure with details of the organization to be fetched from db or
created. Must be a closure to ensure it's executed within a test case
Parameters
----------
kwargs
the details of the desired organization
Returns
-------
Closure
which returns the desired organization
"""

def get_org():
org, new = models.Organization.objects.get_or_create(**kwargs)
return org

return get_org

my_org = organization_get_or_create(label='My Organization')

asset1_recipe = Recipe(Asset,
organization=my_org,
label='asset 1')
asset2_recipe = Recipe(Asset,
organization=my_org,
label='asset 2')

并且可以创建任意数量的 Assets :

>> asset1 = asset1_recipe.make()
>> asset2 = asset2_recipe.make()

最佳答案

这似乎是不可能的(至少目前是这样),但是这个特性已经被讨论过了。看这里:Reference to same foreign_key object

关于django - 模型妈妈 : Multiple recipes with foreign key relation to a single recipe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39098510/

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