Search code examples
pythondjangodjango-rest-frameworkdjango-jsonfield

django REST framework does not work show new fields in a model


I am new to Django, I am trying to achieve a Product Lookup module fetches data from MySQL responds to GET request.

Here is my model

models.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)  

serializers.py

class BrandSerializer(serializers.ModelSerializer):
    class Meta:
        model = CNF_BRAND

GET response

{
  "Status": "SUCCESS",
  "Brand": [
    {
      "COMPANY_NAME": "Test",
      "BRAND_NAME": "Test Brand",
      "BRAND_DESC": "Classic",
    }
  ]
}

views.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")

which works fine.

Updated Code

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)

GET response

{
  "Status": "SUCCESS",
  "Brand": [
    {
      "COMPANY_NAME": "Test",
      "BRAND_NAME": "Test Brand",
      "BRAND_DESC": "Classic",
    }
  ]
}

No Change in the Get Response. I did

python manage.py makemigrations

python manage.py migrate

restarted the django server multiple times

I can see the new fields in database & updated the field values. But unable to see the new fields in my response.

Updated

serializers.py

class BrandSerializer(serializers.ModelSerializer):
 PRODUCT = ProductSerializer(many=True)
  class Meta:
   model = CNF_BRAND
   fields = '__all__' 

Print

Even though the above problem exists, i can print the corresponding values in console

print(brand[0].TITLE)
print(brand[0].FAV_ICON)

Console

My Title
https://www.google.co.in/images/branding/product/ico/googleg_lodp.ico

The response not received in Rest client

GET response

{
  "Status": "SUCCESS",
  "Brand": [
    {
      "COMPANY_NAME": "Test",
      "BRAND_NAME": "Test Brand",
      "BRAND_DESC": "Classic",
    }
  ]
}

Solution

  • Just removed the migrations folder & recreated the database (Since it is in initial stage) then ran the commands

    python manage.py makemigrations
    
    python manage.py migrate
    

    Now it supports further newly added fields