如何使用反向外键创建一个新的Django实例而不将其保存到DB?

时间:2022-11-25 08:14:44

Suppose I have the following 2 Django models:

假设我有以下2个Django模型:

class MyModelA(models.Model):
    my_int = models.IntegerField()

class MyModelB(models.Model):
    my_int = models.IntegerField()
    my_a = models.ForeignKey(MyModelA, related_name="MyModelB_a")

I can create instances of MyModelA in two ways:

我可以通过两种方式创建MyModelA的实例:

# First technique:
>>> a = MyModelA.objects.create(my_int=5)

# Second technique:
>>> a = MyModelA(my_int=5)
>>> a.save()

If I know that I won't need to change a between instantiation and saving, I go with the first technique (to reduce lines of code). If I know that a will be changing between instantiation and saving, I usually go with the second technique shown above (to minimize database accesses).

如果我知道我不需要在实例化和保存之间进行更改,那么我将使用第一种技术(减少代码行)。如果我知道在实例化和保存之间会发生变化,我通常会使用上面显示的第二种技术(以最小化数据库访问)。

Analogously when I want to create an instance of MyModelB from a method within MyModelA through the reverse-foreign-key relationship, I do the following:

类似地,当我想通过反向外键关系从MyModelA中的方法创建MyModelB的实例时,我执行以下操作:

# First technique:
>>> b = self.MyModelB_a.create(my_int=6)

But I don't know how to create instances of MyModelB through the reverse-foreign-key relationship using the second technique. How to do it? What function to call? I want to create an instance using the related_name MyModelB_a without saving it to the DB until I explicitly call save()

但我不知道如何使用第二种技术通过反向外键关系创建MyModelB的实例。怎么做?什么功能要打电话?我想使用related_name MyModelB_a创建一个实例而不将其保存到DB,直到我显式调用save()

# Second technique:
>>> b = a.MyModelB_a.WHAT-GOES-HERE(my_int=6)
>>> b.save()

1 个解决方案

#1


1  

If I understand what you mean .. you want something like a.MyModelB_a.new(my_int=6). Something like .build in rails for example! I'm afraid that's not exist in django.

如果我明白你的意思..你想要像a.MyModelB_a.new(my_int = 6)这样的东西。像rails中的.build之类的东西!我担心django中不存在。

But if you just don't want to import MyModelB for some reason, you can use a work around if you are OK with that.

但是如果你因某些原因不想导入MyModelB,那么你可以使用一个解决方法,如果你对它好的话。

You can use a.MyModelB_a.model(my_int=6, my_a=a)

你可以使用a.MyModelB_a.model(my_int = 6,my_a = a)

Edit:

编辑:

Or you can override Manager class to implement your own method. I didn't try to override RelatedManager before, but it appears that it's allowed in django by use_for_related_fields.

或者,您可以覆盖Manager类以实现自己的方法。我之前没有尝试覆盖RelatedManager,但看起来它是由use_for_related_fields在django中允许的。

#1


1  

If I understand what you mean .. you want something like a.MyModelB_a.new(my_int=6). Something like .build in rails for example! I'm afraid that's not exist in django.

如果我明白你的意思..你想要像a.MyModelB_a.new(my_int = 6)这样的东西。像rails中的.build之类的东西!我担心django中不存在。

But if you just don't want to import MyModelB for some reason, you can use a work around if you are OK with that.

但是如果你因某些原因不想导入MyModelB,那么你可以使用一个解决方法,如果你对它好的话。

You can use a.MyModelB_a.model(my_int=6, my_a=a)

你可以使用a.MyModelB_a.model(my_int = 6,my_a = a)

Edit:

编辑:

Or you can override Manager class to implement your own method. I didn't try to override RelatedManager before, but it appears that it's allowed in django by use_for_related_fields.

或者,您可以覆盖Manager类以实现自己的方法。我之前没有尝试覆盖RelatedManager,但看起来它是由use_for_related_fields在django中允许的。