0

I'm creating something like a blog post, the blog post can contain multiple images. To be able to include a variable number of images, I created a new model called Image. This model contains a ForeignKey to the user that owns it, and an ImageField.

Code:

class Image(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE
    )
    image = models.ImageField(upload_to=get_image_upload_path)

In the post model, I have a ManyToManyField for the Image model. this way, I can have a variable number of images.

images = models.ManyToManyField('Image', blank=True)

I'm trying to test my code. In one of the tests, I'm trying to create a couple of images, then create a post with the images being passed to in a list.

How do I create an instance of the model Image within the tests and provide it an image?

image = Image.objects.create(user=self.user, image=...)

What should be written instead of ... here?

1

1 Answer 1

1

You just need to provide the path the image file you are going to use for testing. Here is how I have done it.

image = Image.objects.create(user=self.user,image='path/to/file.png')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.