Django 包含图像、文件和标记的模型的装置数据

Django 包含图像、文件和标记的模型的装置数据,django,django-models,django-taggit,django-fixtures,easy-thumbnails,Django,Django Models,Django Taggit,Django Fixtures,Easy Thumbnails,我使用的是Djano 3.1、Python 3.6、easy thumbnails 2.7和django taggit 1.3 我想为我的模型创建一个装置数据文件 这是我的(简化)模型: myapp/models.py myapp/fixtures/sample_data.json 如何在JSON fixtures文件中指定文件?如果您尝试通过admin界面使用图像(例如image.png)保存Post,然后查看数据库,您会发现Post的图像是以其相对路径保存的:uploads/Post/fea

我使用的是Djano 3.1、Python 3.6、easy thumbnails 2.7和django taggit 1.3

我想为我的模型创建一个装置数据文件

这是我的(简化)模型:

myapp/models.py myapp/fixtures/sample_data.json
如何在JSON fixtures文件中指定文件?

如果您尝试通过
admin
界面使用图像(例如
image.png
)保存
Post
,然后查看数据库,您会发现Post的图像是以其相对路径保存的:
uploads/Post/featured\u image/image.png
,因此,在设备中,需要指定该路径

在您的
myapp/fixtures/sample_data.json
fixture文件中,它应该是

[
    {
      "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",
      }
    }
]

这只是将文件名的字符串加载到数据库Fixture用于预填充数据库(),然后可以手动移动文件,甚至可以使用自定义命令更好地移动文件。看看这个包,它可能很有用
[
    {
      "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": ???
      }
    }
]
[
    {
      "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",
      }
    }
]