0

Was fighting yesterday with the issue of importing pics from URL to the Django model. Was able to come up with a working solution, but still don't know how this is works. How a save func knows what kind of *args it could process and in what order? Because when I changed places for picture object and filename it didn't work TypeError: join() argument must be str or bytes, not 'File'. Could not understand it reading the docs - https://docs.djangoproject.com/en/2.1/_modules/django/db/models/base/#Model.save. The script below putting NHL players names, id and profile pics to my Player model. Any help?

the commands file:

import urllib.request as urllib
import requests

from django.core.management.base import BaseCommand, CommandError
from django.core.files import File

from players.models import Player


URL_PLAYERS = 'http://www.nhl.com/stats/rest/{}'
URL_PICS = 'https://nhl.bamcontent.com/images/headshots/current/168x168/{}.jpg'


class Command(BaseCommand):

    def import_player(self, data):
        id_ = data["playerId"]
        content = urllib.urlretrieve(URL_PICS.format(id_))
        pic = File(open(content[0], 'rb'))  # do I need to close the file here?
        file = f'{data["playerName"]}.jpg'
        player = Player(name=data["playerName"], nhl_id=id_)
        player.save()
        player.image.save(file, pic)


    def handle(self, *args, **options):

        params = {"isAggregate": "false",
                  "reportType": "basic",
                  "isGame": "false",
                  "reportName": "skaterpercentages",
                  "cayenneExp": "gameTypeId=2 and seasonId=20182019"}

        response = requests.get(url=URL_PLAYERS.format("skaters"),
                                params=params)

        response.raise_for_status()
        data = response.json()["data"]

        for player in data:
            self.import_player(player)

the models file:

from django.db import models

class Player(models.Model):
    name = models.CharField(max_length=128)
    nhl_id = models.IntegerField()  #(unique=True)
    image = models.ImageField(default='default.jpg', upload_to='players_pics')

    def __str__(self):
        return f'{self.name}'
Brown Bear
  • 19,655
  • 10
  • 58
  • 76
edvard_munch
  • 266
  • 1
  • 5
  • 17
  • 1
    You need to show the full traceback. Which save method are you talking about? Are you sure you shouldn't be looking at the [FileField save method](https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.fields.files.FieldFile.save)? – Daniel Roseman Dec 21 '18 at 08:10
  • My god, this is a facepalm. Despite all of the googling yesterday I didn't even know that another save method exists. This is all so clear now. The question should be closed know. Thanks for the comment. Just started to dig into Django this week. – edvard_munch Dec 21 '18 at 08:37

1 Answers1

0

Just to not to leave the question unanswered. As @Daniel Roseman suggested I confused two different methods. Was actually using the FileField save method, but thought I was using Model.save method. So, was looking at the wrong documentation.

edvard_munch
  • 266
  • 1
  • 5
  • 17