Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access params in methods (post_create) #1098

Open
Zerotask opened this issue Sep 23, 2024 · 0 comments
Open

Access params in methods (post_create) #1098

Zerotask opened this issue Sep 23, 2024 · 0 comments

Comments

@Zerotask
Copy link

Zerotask commented Sep 23, 2024

The problem

I'd like to pass an argument which is not part of the model, but used in the post_create method to create another object

Proposed solution

  1. define it a a regular field and then set in in Meta's exclude would be one option
  2. define it in Params and make this available, e.g. self.params.xxx

Example

class MyFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = MyModel
        
    class Params:
        with_reminder = False
        
    first_name = factory.Faker("first_name")
    last_name = factory.Faker("last_name")
    
    def post_create(self, create, extracted, **kwargs):
        if create and self.params.with_reminder:
            Reminder.objects.create(related_object=self)


MyFactory() # no reminder
MyFactory(with_reminder=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant