[英]Fixtures data for model containing images, files and tags
我正在使用 Djano 3.1、Python 3.6、easy-thumbnails 2.7 和 django-taggit 1.3
我想為我的模型創建一個夾具數據文件。
這是我的(簡化)模型:
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": ???
}
}
]
如何在我的 JSON 固定裝置文件中指定文件?
如果您嘗試通過admin
界面保存帶有圖像的Post
,例如image.png
然后查看數據庫,您會發現帖子的圖像是使用其相對路徑保存的: uploads/post/featured_image/image.png
,所以在您需要指定該路徑的裝置。
在您的myapp/fixtures/sample_data.json
夾具文件中,它應該是這樣的
[
{
"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",
}
}
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.