Search code examples
pythondjangomodels

How to create a record from one to one relationship reverse direction in Django?


Django offers Related objects for oneToMany or manyToMany relationship.

Using this object, It can create a record from the reverse direction.

(for example with XXXX_set.create(.....) or XXXX_set.get_or_create(.....))

I want to use this kind of function with an oneToOne relationship.

Is there any way to create an one to one relationship record from the reverse direction ?

for example

If class A(models.Models) and class B(models.Model) are tied by one to one relationship and I create A and save() it, than I want to create B also through A


Solution

  • No, there is no way of doing that because there is no queryset between class A and class B, like it was with ManyToMany or ForeignKey. You must create B directly and assign A to proper field on B.