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:
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')
[
{
"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 admin
interface to save a Post
with an image e.g image.png
and 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.json
fixture file it should be like
[
{
"model": "myapp.Post",
"pk": 1,
"fields": {
"featured_image": "uploads/post/featured_image/FEATURED_IMAGE.EXT",
"content": "This is where the content goes",
}
},
{
"model": "myapp.PostFileAttachment",
"pk": 1,
"fields": {
"post": 1,
"file": "uploads/post/attachments/ATTACHMENT.EXT",
}
},
{
"model": "myapp.PostImageGallery",
"pk": 1,
"fields": {
"post": 1,
"description": "File description",
"image": "uploads/blogpost/gallery/IMAGE.EXT",
}
}
]