Skip to content

Instantly share code, notes, and snippets.

@piotrkilczuk
Last active May 20, 2021 10:54
Show Gist options
  • Save piotrkilczuk/c870a02ef16e7579a4bcfac5545a3c0d to your computer and use it in GitHub Desktop.
Save piotrkilczuk/c870a02ef16e7579a4bcfac5545a3c0d to your computer and use it in GitHub Desktop.
factories_v4.py
class PlayerFactory(DjangoModelFactory):
# other declarations removed for brevity
@post_generation
def picture(self, create, extracted, **kwargs):
if extracted:
self.picture = extracted
elif kwargs:
assert 'filename' in kwargs, 'You need to pass in the filename'
location = os.path.join(settings.BASE_DIR, f'fbd/championship/fixtures/{kwargs["filename"]}')
assert os.path.isfile(location), f'{location} does not exist'
self.picture = File(open(location, 'rb'))
if create:
self.save(update_fields=['picture'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment