Vignesh A
Vignesh A

Reputation: 31

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",
    }
  ]
}

Upvotes: 0

Views: 1858

Answers (3)

Vignesh A
Vignesh A

Reputation: 31

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

Upvotes: 0

zaidfazil
zaidfazil

Reputation: 9235

Add a fields attribute to meta class of the serializer,

fields = [f.name for f in self.fields]

Or,

fields = ('COMPANY_NAME', 'BRAND_NAME', 'BRAND_DESC', 'TITLE', 'FAV_ICON')

Then try the response again.

EDIT

The problem I think that you are only looking at the response objects, which are created before the migration. I think the objects in the response has only the fields in the previous migration. They don't have a TITLE or FAV_ICON, that's why the response had only the previous fields. Inorder to get the new fields in the response, you should create new objects and then try to request the response, which should give the appropriate fields.

You could also give the fields some default values, if the existing objects are required to have these fields. Default values can be assigned in the model field options. For further details, see the Django documentation for model field reference.

Here https://docs.djangoproject.com/en/1.11/ref/models/fields/

Upvotes: 2

Dawid Dave Kosiński
Dawid Dave Kosiński

Reputation: 901

Basically @Fazil_Zaid answer is correct. Maybe your View is wrong. You are using a modelSerializer so you can write a APIView view like:

class FlightList(APIView):
    u"""View of flights."""

    def perform_create(self, serializer):
        u"""Pre-create method."""
        serializer.save(added_by=self.request.user)

    def get(self, request, format=None):
        u"""Get flights on HTTP GET."""
        flights = Flight.objects.all()
        serializer = FlightSerializer(flights, many=True)
        return Response(serializer.data)

This is explained in the Rest documentation in section 3

Upvotes: 0

Related Questions