I am using Djano 3.1, Python 3.6, easy-thumbnails 2.7 and django-taggit 1.3
I want to create a fixtures data file for my model.
Here is my (simplified) model:
myapp/models.py
class Post(models.Model):
featured_image = ThumbnailerImageField(upload_to='uploads/post/featured_image', blank=True, null=True)
content = models.CharField(max_text=1000, null=False, blank=False)
tags = TaggableManager()
class PostFileAttachment(models.Model):
post = models.ForeignKey(Post, related_name='attachments', on_delete = mFixtures data for model containing images and filesodels.CASCADE)
file = models.FileField(upload_to="uploads/post/attachments")
class PostImageGallery(models.Model):
post = models.ForeignKey(Post, related_name='pictures', on_delete = models.CASCADE)
description = models.CharField(max_length=100, blank=True, null=True, default='')
image = models.ImageField(upload_to='uploads/blogpost/gallery')
myapp/fixtures/sample_data.json
[
{
"model": "myapp.Post",
"pk": 1,
"fields": {
"featured_image": ???
"content": "This is where the content goes"
"tags": ???
}
},
{
"model": "myapp.PostFileAttachment",
"pk": 1,
"fields": {
"post": 1
"file": ???
}
},
{
"model": "myapp.PostImageGallery",
"pk": 1,
"fields": {
"post": 1
"description": "File description",
"image": ???
}
}
]
How do I specify files in my JSON fixtures file?
if you try via
admininterface to save aPostwith an image e.gimage.pngand then you look the database, you will find that the post's image was saved with its relative path :uploads/post/featured_image/image.png, so in your fixture you need to specify that path.in your
myapp/fixtures/sample_data.jsonfixture file it should be like