Django休息framewrok如何查询关系

时间:2015-10-04 11:10:06

标签: django django-rest-framework

models.py

class Employee(models.Model):
    Name = models.CharField(max_length=50 )
    Email = models.CharField(max_length=30)

    def __str__(self):
        return self.Name              # __unicode__ on Python 2



class EmployeeProfile(models.Model):
    employee = models.ForeignKey(Employee, related_name='fromemployee')
    Address = models.CharField(max_length=50, null=True, blank=True)
    job_title = models.CharField(max_length=50, null=True, blank=True)
    employeesomething= models.ForeignKey(Employee, related_name='refirence')

    def __str__(self):              # __unicode__ on Python 2
       return self.Address



serializers.py

class EmployeeSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Employee
        fields = ('id', 'Name')


class EmployeeProfileSerializer(serializers.HyperlinkedModelSerializer):
    # usergroups = serializers.StringRelatedField(many=True)
    employee_set = EmployeeSerializer(many=True, read_only=True)
    class Meta:
        model = EmployeeProfile
        fields = ('id', 'Address','employee','job_title','employeesomething','employee_set')


views.py

class EmployeeViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows groups to be viewed or edited.
    """
    queryset = Employee.objects.all()
    serializer_class = EmployeeSerializer    

class EmployeeProfileViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows groups to be viewed or edited.
    """
    queryset = EmployeeProfile.objects.all()
    serializer_class = EmployeeProfileSerializer        


urls.py

router = routers.DefaultRouter()


router.register(r'employee', views.EmployeeViewSet)
router.register(r'employeeprofile', views.EmployeeProfileViewSet)


urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]


my out put   http://localhost:8000/employeeprofile/

[
    {
        "id": 1,
        "Address": "Hyderabd",
        "employee": "localhost:8000/employee/1/",
        "job_title": "admin",
        "employeesomething": "http://localhost:8000/employee/2/"
    },
    {
        "id": 2,
        "Address": "Hyderabd",
        "employee": "http://localhost:8000/employee/1/",
        "job_title": "admin",
        "employeesomething": "http://localhost:8000/employee/3/"
    },
    {
        "id": 3,
        "Address": "Hyderabd",
        "employee": "http://localhost:8000/employee/4/",
        "job_title": "admin",
        "employeesomething": "http://localhost:8000/employee/5/"
    },
    {
        "id": 4,
        "Address": "Hyderabd",
        "employee": "http://localhost:8000/employee/4/",
        "job_title": "admin",
        "employeesomething": "http://localhost:8000/employee/6/"
    }
]

*     Employee和EmployeeProfile之间有关系如果我传递Employe Id我需要得到Employee Profile如何查询请帮助我。它是passbul使用viewsets.ModelViewSet获取查询谢谢     *

1 个答案:

答案 0 :(得分:0)

在您看来:

假设您在employee_id

中获得request.data
from rest_framework.views import APIView
from rest_framework.response import Response
from myapp.serializers import EmployeeProfileSerializer
from myapp.models import EmployeeProfile


class MyView(APIView):

    def get(self, request, *arga, **kwargs)
        e_profiles = EmployeeProfile.objects.filter(employee__pk=request.GET.get('employee_id'))

        # Then call you serializer

        e_serialized = EmployeeProfileSerializer(e_profiles, many=True)
        return Response(e_serialized.data)