1.python打印异常信息
try:
game = Game.objects.get(game_id=game_id, language=language)
print(game.pk)
except:
print(sys.exc_info())
2.Django将imagefield序列化以获取完整的URL
model.py
class UserG(models.Model):
username = models.CharField(max_length=128, default='luyou', unique=True)
icon = models.ImageField(upload_to='./static/images/%Y%m%d/',null=True,
max_length=255,default="static/images/20160924/default.png")
Serializers.py
class UserGSerializer(serializers.ModelSerializer):
icon = serializers.SerializerMethodField()#加上这行后必须添加get_icon()方法否则报错
def get_icon(self, userg):
return str(userg.icon)
class Meta:
model = UserG
fields = ('username', 'icon')
views.py
class UserGDetail(APIView):
def get(self, request, format=None):
serializer = UserGSerializer(userg)
data = serializer.data
return Response(data)
使用SerializerMethodField后能够返回完整得URL否则url中得:将会变成%3A返回。