[d-5]django上传图片并使用postman测试

2020-04-17  本文已影响0人  ALLIN5

说明:使用django模型类ImageFields字段上传图片到服务器本地,并使用postman测试

class People(BaseModel):
  
    name = models.CharField(verbose_name='姓名', max_length=1024, default="",blank=True, null=True)
    age = models.CharField(verbose_name='年龄',max_length=30,blank=True, null=True,help_text='年龄')
    face = models.ImageField(upload_to='pic')
    create_time = models.CharField(max_length=30,verbose_name='时间', blank=True, null=True)

    class Meta:
        db_table = 'people'
        verbose_name = '人物信息'
        verbose_name_plural = verbose_name
class PeopleSerializer(serializers.ModelSerializer):
    face = serializers.ImageField(max_length=None, use_url=True)

    class Meta:
        model = People
        fields = ("__all__")
class PeopleViewSet(ModelViewSet):

    lookup_field = 'pk'
    serializer_class = PeopleSerializer

urls.py

router = DefaultRouter()
router.register('adddata', views.PeopleViewSet,basename='people')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = 'media/'
The number of GET/POST parameters exceeded settings.DATA_UPLOAD_MAX_NUMBER_FIELDS错误
DATA_UPLOAD_MAX_NUMBER_FIELDS = 10240

{
    "code": 201,
    "message": "保存成功",
    "data": {
        "id": 15,
        "face": "http://0.0.0.0:8000/people/adddata/media/pic/dubai-5041154_1920.jpg",
        "update_time": "2020-04-17T10:06:17.418673",
        "gender": "1",
    }
}
上一篇下一篇

猜你喜欢

热点阅读