Search code examples
pythonjsondjangodjango-fixtures

How to get django to upload an image file via fixtures


I have a class in my models.py:

class Car(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    create_date = models.DateTimeField('date added', auto_now_add=True)
    modify_date = models.DateTimeField('date modified', default=timezone.now)
    name = models.CharField(max_length=200)
    image_file = models.ImageField(
        upload_to=upload_to_company, null=True, blank=True)

and a JSON for fixtures like this:

   [
      {
        "model": "polls.car",
        "fields": {
            "id":"09734145-7a15-4c84-8b7c-eaab4112cbaa",
            "name": "VW",
            "create_date": "2015-01-01T00:00:00+00:00"
        }
      }
    ]

which I load as a fixture. Now I want it to get it to load an image as well, however when I insert "image_file":"VW.jpg" (VW.jpg is in the same folder as the JSON). VW.jpg shows up in the admin's panle but it doesn't upload the actual picture.

Anyone an idea or a hint for where to look?


Solution

  • No idea why I didn't come earlier to this conclusion but it works if you just place the file with name specified in the fixtures file in the directory you want to have it. For example:

     [
          {
            "model": "polls.car",
            "fields": {
                "id":"09734145-7a15-4c84-8b7c-eaab4112cbaa",
                "name": "VW",
                "create_date": "2015-01-01T00:00:00+00:00",
                "image_file":"VW.jpg"
            }
          }
        ]
    

    and then put VW.jpg where your media directory is located, for example:

    media/VW.jpg

    It doesn't get uploaded this way but it's in the database and accessible over the admins panel