Discussion:
how to write routers for the modelviewset based view function
Jun Ren
2018-10-06 22:35:22 UTC
Permalink
Hello, I defined a model as following, and writ a view based on the ModelViewset, also overidden the get_query function, as folloing:



class MachineStatus(models.Model):
Soft_ID = models.CharField(max_length=20)
operator_jun = models.CharField(max_length=20)
end_date = models.DateTimeField('date expired')
start_date = models.DateTimeField('date registeres')
machine_code = models.CharField(max_length=35)


class MachineStatusViewSetNew(viewsets.ModelViewSet):
serializer_class = MachineStatusSerializer
pagination_class = MachineStatusSetPagination

def get_queryset(self):
machine_code = self.request.data["mac_code"]
if machine_code:
queryset = MachineStatus.objects.all().filter(machine_code=machine_code)

return queryset



Question:

How to write the url for this view?


I tried following:


router.register(r'machine_status_query/(?P<mac_code>\d+)$', views.MachineStatusViewSetNew, base_name="xxx")


And typed the url "http://0.0.0.0:8000/Appauth/machinestatusviewset/machine_status_query/60" ?


it doesnot work for me? How to make it work? Any help is appreciated.


Page not found (404)
Request Method: GET
Request URL: http://0.0.0.0:8000/Appauth/machinestatusviewset/machine_status_query/60
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jason
2018-10-07 12:36:50 UTC
Permalink
Are you sure you have a record in the db with machine_code = 60?

your model defines that field as a CharField, so using an integer in the
URL makes me suspect you're using a primary key, which is very different
from the definition of get_queryset
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jun Ren
2018-10-08 02:49:02 UTC
Permalink
I solved it , thanks your reply.
Post by Jason
Are you sure you have a record in the db with machine_code = 60?
your model defines that field as a CharField, so using an integer in the
URL makes me suspect you're using a primary key, which is very different
from the definition of get_queryset
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...