android - Adding a custom DateTimeField that can be updated through a PUT request (DjangoRestFramework) -
i'm new django , created django app following tutorial: http://www.django-rest-framework.org/tutorial/quickstart/
this tutorial gives pre-made user class includes various fields. trying add custom datetimefield can updated through put request in android. how add custom field "timestamp" class.
here code:
serializers.py
from django.contrib.auth.models import user rest_framework import serializers django.contrib.auth.models import models class userserializer(serializers.hyperlinkedmodelserializer): class meta: model = user lookup_field = 'username' set_timezone = 'date_joined' fields = ('url', 'username','date_joined')
views.py
from django.contrib.auth.models import user rest_framework import viewsets tutorial.quickstart.serializers import userserializer django.conf import settings django.shortcuts import redirect, render dir(settings) settings.__dict__ class userviewset(viewsets.modelviewset): """ api endpoint allows users viewed or edited. """ lookup_field = 'username' queryset = user.objects.all() serializer_class = userserializer
urls.py
from django.conf.urls import url, include rest_framework import routers tutorial.quickstart import views router = routers.defaultrouter() router.register(r'users', views.userviewset) urlpatterns = [ url(r'^', include(router.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) ]
models.py
from django.db import models # create models here. datetime import datetime class time(models.model): timestamp = models.datetimefield(auto_now_add=true) def __unicode__(self): return "time"
error
exception value:
field nametimestamp
not valid modeluser
. exception location: /library/python/2.7/site-packages/rest_framework/serializers.py in build_unknown_field, line 1079 python executable: /library/frameworks/python.framework/versions/2.7/resources/python.app/contents/macos/python python version: 2.7.6
you should better use abstractbaseuser
django in order construct own user model. more simple creating new dedicated time
model, , drf custom relational field goes it.
for example:
from django.contrib.auth.models import abstractbaseuser django.db import models class customuser(abstractbaseuser): timestamp = models.datetimefield()
then, in drf serializer:
from rest_framework import serializers class customuserserializer(serializers.hyperlinkedmodelserializer): class meta: model = customuser lookup_field = 'username' set_timezone = 'date_joined' fields = ('url', 'username','date_joined', 'timestamp')
Comments
Post a Comment