无法理解 Django save() 如何处理 *args
Trouble understanding how the Django save() handles *args
昨天正在为将图片从 URL 导入 Django 模型的问题而苦恼。能够提出一个可行的解决方案,但仍然不知道它是如何工作的。 save
func 如何知道它可以处理什么样的 *args 以及以什么顺序处理?因为当我更改图片对象和文件名的位置时它不起作用 TypeError: join() argument must be str or bytes, not 'File'
。无法理解阅读文档 - https://docs.djangoproject.com/en/2.1/_modules/django/db/models/base/#Model.save。下面的脚本将 NHL 球员的姓名、ID 和个人资料照片添加到我的球员模型中。有帮助吗?
命令文件:
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)
模型文件:
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}'
只是为了不让问题悬而未决。正如@Daniel Roseman 建议的那样,我混淆了两种不同的方法。居然用了FileField save method, but thought I was using Model.save method。因此,正在查看错误的文档。
昨天正在为将图片从 URL 导入 Django 模型的问题而苦恼。能够提出一个可行的解决方案,但仍然不知道它是如何工作的。 save
func 如何知道它可以处理什么样的 *args 以及以什么顺序处理?因为当我更改图片对象和文件名的位置时它不起作用 TypeError: join() argument must be str or bytes, not 'File'
。无法理解阅读文档 - https://docs.djangoproject.com/en/2.1/_modules/django/db/models/base/#Model.save。下面的脚本将 NHL 球员的姓名、ID 和个人资料照片添加到我的球员模型中。有帮助吗?
命令文件:
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)
模型文件:
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}'
只是为了不让问题悬而未决。正如@Daniel Roseman 建议的那样,我混淆了两种不同的方法。居然用了FileField save method, but thought I was using Model.save method。因此,正在查看错误的文档。