0

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",
    }
  ]
}
Vignesh A
  • 31
  • 7

3 Answers3

2

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/

zaidfazil
  • 9,017
  • 2
  • 24
  • 47
0

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

  • updated my views.py to class BrandList(APIView): def get(self, request, format=None): brands = CNF_BRAND.objects.all() serializer = BrandSerializer(brands, many=True) return Response(serializer.data) Added urls.py with url(r'^brands/$', views.BrandList.as_view()), still no luck..... – Vignesh A May 13 '17 at 12:48
  • I recommend doing the how package from the beginning. I think that you have a problem that you didn't mention in the question or a normal typo. Did you access the page in incognito mode? another option is backwarding your migrations via ``python manage.py migrate APP zero``. The zero parameter will delete all migrations on a specyfic APP, even the initial. Then delete the migrations and run again ``python manage.py makemigrations APP`` and ``python manage.py migrate APP``. – Dawid Dave Kosiński May 15 '17 at 07:48
0

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

Vignesh A
  • 31
  • 7