我是 Django 新手,我正在尝试实现产品查找模块从 MySQL 获取数据以响应 GET 请求。
这是我的模型
模型.py
class CNF_BRAND(models.Model):
COMPANY_NAME = models.CharField(max_length=255)
BRAND_NAME = models.CharField(max_length=255)
BRAND_DESC = models.CharField(max_length=1024)
序列化程序.py
class BrandSerializer(serializers.ModelSerializer):
class Meta:
model = CNF_BRAND
获取响应
{
"Status": "SUCCESS",
"Brand": [
{
"COMPANY_NAME": "Test",
"BRAND_NAME": "Test Brand",
"BRAND_DESC": "Classic",
}
]
}
视图.py
response_data = {}
brand=CNF_BRAND.objects.all() #Main Cone #Man Goods
serializedProduct = BrandSerializer(brand, many=True)
response_data['Brand'] = serializedProduct.data
response = JsonResponse(response_data, status=status.HTTP_200_OK)
return HttpResponse(response,content_type="application/json")
效果很好。
更新代码
class CNF_BRAND(models.Model):
COMPANY_NAME = models.CharField(max_length=255)
BRAND_NAME = models.CharField(max_length=255)
BRAND_DESC = models.CharField(max_length=1024)
TITLE = models.CharField(max_length=21)
FAV_ICON = models.URLField(max_length=1024)
获取响应
{
"Status": "SUCCESS",
"Brand": [
{
"COMPANY_NAME": "Test",
"BRAND_NAME": "Test Brand",
"BRAND_DESC": "Classic",
}
]
}
获取响应没有变化。我做了
python manage.py makemigrations
python manage.py 迁移
多次重启django服务器
我可以看到数据库中的新字段并更新了字段值。但无法在我的回复中看到新字段。
更新
序列化程序.py
class BrandSerializer(serializers.ModelSerializer):
PRODUCT = ProductSerializer(many=True)
class Meta:
model = CNF_BRAND
fields = '__all__'
打印
即使存在上述问题,我也可以在控制台中打印相应的值
print(brand[0].TITLE)
print(brand[0].FAV_ICON)
安慰
My Title
https://www.google.co.in/images/branding/product/ico/googleg_lodp.ico
Rest客户端未收到响应
获取响应
{
"Status": "SUCCESS",
"Brand": [
{
"COMPANY_NAME": "Test",
"BRAND_NAME": "Test Brand",
"BRAND_DESC": "Classic",
}
]
}