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}'
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.