On , I learnt ...

About Factory Boy’s SelfAttribute class

Factory Boy has a useful SelfAttribute class that lets a field reference another field of the object being constructed.

It’s particularly useful when used in a SubFactory as it lets you reference fields on the parent object. E.g.

import factory
from django.utils import timezone

class Message(factory.Factory):
    sent_at = factory.LazyFunction(timezone.now)

class Event(factory.Factory):
    occurred_at = factory.LazyFunction(timezone.now)

    message = factory.SubFactory(
        # Ensure the sent_at of the linked message matches the occurred_at field
        # of the event.

event = Event()
assert event.occurred_at == event.message.sent_at  # True