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

FileField and ArrayField not generated with random data when default=None or default=list #356

Open
legshort opened this issue Oct 17, 2022 · 1 comment

Comments

@legshort
Copy link

Describe the issue

Generating random data is not working below the condition.
ArrayField with default=list
FileField with default=None

To Reproduce

class A(models.Model):
    career = ArrayField(models.CharField(max_length=30), default=list)
    main_image = models.ImageField(blank=True, null=True, default=None)

a = baker.make(A, _fill_optional=True, _create_files=True)
print(a.career, a.main_image)

[], None

Expected behavior
ArrayField and FileField should be filled with random data.

Versions

  • Python: 3.10.3
  • Django: 4.1.2
  • Model Bakery: 1.8.0
@kiranrokkam09
Copy link

kiranrokkam09 commented Oct 5, 2023

hi @legshort we can use below function and solve the above issue.

def generate_random_a():
    return A(
        career=[fake.word() for _ in range(3)],  # Generate a list of 3 random words
        main_image=baker.make('PILImage')  # Generate a random image
    )

a = baker.make(A, _fill_optional=True, _model_instance_generator=generate_random_a)

if you like the solution please assign me the issue.

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

2 participants