Django REST Framework ModelField requires "model_field" kwarg

I'm kind of new to the Django REST Framework. I get this strange error, which is probably caused by something simple, I write β€œstrange” because I searched for it several times and could not find any link.

Model:

class Records(models.Model): owner = models.ForeignKey(User, null=True) activity = models.ForeignKey(Activity, null=True) time_start = models.DateTimeField(null=True) time_end = models.DateTimeField(null=True) ... 

to serializer:

 class RecordSerializer(serializers.ModelField): activity = serializers.PrimaryKeyRelatedField() now = datetime.today() owner = serializers.CharField(source='owner.username', read_only=True) time_start = serializers.DateTimeField(source='now') class Meta: model = Records fields = ("owner", "activity", "time_start") 

View:

 class StartApiView(generics.CreateAPIView): model = Records serializer_class = RecordSerializer def pre_save(self, obj): obj.owner = self.request.user 

URLs:

 urlpatterns = patterns('', # Today app url(r'^today/$', views.TodayView.as_view(), name='today'), url(r'^start/$', views.StartApiView.as_view(), name='start'), ... 

A POST request comes from the backbone, and JSON all has the value dict: {"activity":"1"} . What am I missing? It is assumed that a new Records object is created in the view with the ForeignKey field activity specified for the activity received in the POST request and saving it.

The error I am getting is:

 ValueError at /times/start/ ModelField requires 'model_field' kwarg 
+1
source share
1 answer

Invalid serializer base class. It should be a ModelSerializer instead of a ModelField.

 from rest_framework import serializers class RecordSerializer(serializers.ModelSerializer): # serializer implementation 
+3
source

Source: https://habr.com/ru/post/978424/


All Articles