0

I have a django model that looks like this:

class AcademicProgramsManager(models.Manager):
    def get_by_natural_key(self, acad_program_id, program_title, required_credits):
        return self.get(acad_program_id = acad_program_id, program_title = program_title, required_credits = required_credits)

class AcademicPrograms(models.Model):

    objects = AcademicProgramsManager()
    acad_program_id = models.IntegerField(primary_key=True)
    acad_program_category = models.ForeignKey(AcademicProgramCategories)
    acad_program_type = models.ForeignKey(AcademicProgramTypes)
    acad_program_code = models.CharField(max_length=25)
    program_title = models.CharField(max_length=64)
    required_credits = models.IntegerField()
    min_gpa = models.FloatField()
    description = models.CharField(max_length=1000)

    def natural_key(self):
        return (self.acad_program_id, self.program_title, self.required_credits)

class StudentAcademicPrograms(models.Model):
    student = models.ForeignKey(Students)
    academic_program = models.ForeignKey(AcademicPrograms)
    credits_completed = models.IntegerField()
    academic_program_gpa = models.FloatField()
    primary_program = models.BooleanField()

This is my serializers.py file:

from studentapp.models import AcademicPrograms, AcademicProgramsManager, StudentAcademicPrograms
from rest_framework import serializers

class StudentAcademicProgramsSerializer(serializers.ModelSerializer):
    class Meta:
        model = StudentAcademicPrograms
    fields = ('credits_completed','academic_program_gpa','primary_program')

class AcademicProgramsSerializer(serializers.ModelSerializer):
    studentacademicprograms = StudentAcademicProgramsSerializer(many = True)

    class Meta:
        model = AcademicPrograms
        fields = ('acad_program_id','program_title','required_credits','studentacademicprograms')

My api.py file looks like this:

from studentapp.models import AcademicPrograms, AcademicProgramsManager, StudentAcademicPrograms
from studentapp.serializers import StudentAcademicProgramsSerializer, AcademicProgramsSerializer
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response

class AcademicProgramsList(APIView):
    def get(self, request, format=None):
        academicprograms = AcademicPrograms.objects.all()
        serialized_academicprograms = AcademicProgramsSerializer(academicprograms, many=True)
        return Response(serialized_academicprograms.data)

class AcademicProgramsDetail(APIView):

    def get_onjects(self, pk):
        try:
            return AcademicPrograms.object.get(pk=pk)
        except AcademicPrograms.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        academicprogram = self.get_object(pk)
        serialized_academicprogram = AcademicProgramsSerializer(academicprogram)
        return Response(serialized_academicprogram.data)

and finally my urls:

from django.conf.urls import patterns, include, url
from rest_framework.urlpatterns import format_suffix_patterns
from studentapp import api

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'texascompletes.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    #API:
    url(r'^api/studentacademicprograms/$', api.AcademicProgramsList.as_view()),
    url(r'^api/studentacademicprograms/(?P<pk>[0-9]+)/$', api.AcademicProgramsDetail.as_view()),

    url(r'^admin/', include(admin.site.urls)),
)

urlpatterns = format_suffix_patterns(urlpatterns)

I am getting the following error when i give my api/studentacademicprograms

'AcademicPrograms' object has no attribute 'studentacademicprograms'

Where am i going wrong?

4
  • I'm not familiar with django-rest-framework, but looking at its docs it seems to me that you shouldn't include studentacademicprograms in the AcademicProgramsSerializer.Meta.fields list — that field list only specifies which actual fields of your model to serialize, but studentacademicprograms is not a field on your AcademicPrograms model. Commented Apr 26, 2014 at 8:14
  • i was following the nested relations from this documentation. Even they used it the same way which got me confused. django-rest-framework.org/api-guide/relations Commented Apr 26, 2014 at 15:40
  • Their examples do not fit your scenario - their models actually contain the fields they set up for serialization. Commented Apr 26, 2014 at 15:56
  • ok..am a total newbie and so i followed the documentation. Can you give me a better way of defining my serializers? Commented Apr 26, 2014 at 15:57

1 Answer 1

1

Try setting the source to the attribute/method name on the model. For example:

studentacademicprograms = StudentAcademicProgramsSerializer(
    many=True, 
    source='studentacademicprograms_set')

The example given in the Django Rest Framework Serializer relations docs sets a related name on the models which matches the attribute name in the serializer (the default). If the names don't match you need to specify them model's source method/attribute to use.

Sign up to request clarification or add additional context in comments.

2 Comments

hi thanks for the response. Am not sure if i am getting you right. Where exactly did u want me add it?
Change the line in AcademicProgramsSerializer that matches above adding the source. You should also remove 'studentacademicprograms' from the fields as it's not required (you've specified it's serialization using an attribute) and it should match the name of the reverse relationship i.e. studentacademicprograms_set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.