DRF IntegrityError: NOT NULL constraint failed: user_id - django

I can't figure out how to pass user object to the following serializer:
class ReviewSerializer(serializers.ModelSerializer):
user = UserSerializer(read_only=True)
class Meta:
model = Review
fields = ('pk', 'title', 'user', 'movie', 'timestamp', 'review_text',)
I have this viewset:
class ReviewsViewSet(viewsets.ModelViewSet):
queryset = Review.objects.all()
serializer_class = ReviewSerializer
and this model:
class Review(models.Model):
title = models.CharField(max_length=255)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='reviews')
movie = models.ForeignKey(Movie, on_delete=models.CASCADE, related_name='reviews')
review_text = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '{movie} review by {user}'.format(user=self.user, movie=self.movie)
My javascript request looks like this:
return axios({
method: 'post',
url: 'http://localhost:8000/api/reviews/',
data: { // Using data from Vue
title: this.review_title,
movie: this.id,
review_text: this.review_text,
user: JSON.stringify(this.user)
},
headers: {
'Content-Type': 'application/json',
Authorization: `JWT ${token}`
}
})
It gives me this traceback.
How should I pass the user object to the request?
Thanks in advance.

Remove read_only=True from serializer
class ReviewSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = Review
fields = ('pk', 'title', 'user', 'movie', 'timestamp', 'review_text',)
If you set read_only=True, the DRF will not takes the value from input source even if it's there
From the doc,
Read-only fields are included in the API output, but should not be
included in the input during create or update operations. Any
'read_only' fields that are incorrectly included in the serializer
input will be ignored.
Set this to True to ensure that the field is used when serializing a
representation, but is not used when creating or updating an instance
during deserialization.
Defaults to False
UPDATE
You should override the create() method of ReviewSerializer as
class ReviewSerializer(serializers.ModelSerializer):
user = UserSerializer()
def create(self, validated_data):
user_dict = validated_data.pop('user')
user_obj, created = User.objects.get_or_create(**user_dict)
return Review.objects.create(user=user_obj, **validated_data)
class Meta:
model = Review
fields = ('pk', 'title', 'user', 'movie', 'timestamp', 'review_text',)
for debug purpose only
class ReviewsViewSet(viewsets.ModelViewSet):
queryset = Review.objects.all()
serializer_class = ReviewSerializer
def create(self, request, *args, **kwargs):
print(request.data) # print here <<<<
return super(ReviewsViewSet, self).create(request, *args, **kwargs)

Related

Django Rest Framework - URL With Query Parameters in Serializer

I have a Story and Post models, where a Post belongs to a Story. I want a URL to get all Posts associated with a given Story.
I was able to override the get_queryset of my PostViewSet in order to filter posts by story with URLs like http://localhost:8000/posts/?story=1/. This works beautifully if I type in the URL directly. Now I want to return this kind of url in my StorySerializer. I would like to be able to get Story responses that look like this
[
{
"url": "http://localhost:8000/stories/1/",
"title": "Hero's Journey",
"openings": 0,
"date_created": "2020-06-28T16:53:35.150630Z",
"posts": "http://localhost:8000/posts/?story=1/"
},
{
"url": "http://localhost:8000/stories/2/",
"title": "Halo 3",
"openings": 0,
"date_created": "2020-06-28T18:17:12.973586Z",
"posts": "http://localhost:8000/posts/?story=2/"
}
]
Is there DRF support for this kind of thing? I was trying to use a HyperlinkedIdentityField with 'post-list' View in my StorySerializer, but I couldn't find a combination of parameters that would work. The current exception I get is
AttributeError: 'Story' object has no attribute 'posts'
Serializers
class StorySerializer(serializers.HyperlinkedModelSerializer):
posts = serializers.HyperlinkedIdentityField(
view_name = 'post-list',
many=True,
lookup_field = 'pk',
lookup_url_kwarg = 'story',
)
class Meta:
model = models.Story
fields = ['url', 'title', 'openings', 'date_created', 'posts']
class PostSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.Post
fields = ['url', 'story', 'user', 'text', 'date_created']
Views
class StoryViewSet(viewsets.ModelViewSet):
queryset = models.Story.objects.all()
serializer_class = serializers.StorySerializer
class PostViewSet(viewsets.ModelViewSet):
queryset = models.Post.objects.all()
serializer_class = serializers.PostSerializer
def get_queryset(self):
queryset = self.queryset
story_id = self.request.query_params.get('story', None)
if story_id is not None:
queryset = queryset.filter(story=story_id)
return queryset
Models
class Story(models.Model):
title = models.CharField(max_length=100)
date_created = models.DateTimeField(default=timezone.now)
openings = models.PositiveSmallIntegerField(default=0)
participant = models.ManyToManyField(User)
class Post(models.Model):
text = models.CharField(max_length=300)
user = models.ForeignKey(
User,
on_delete=models.PROTECT)
story = models.ForeignKey(
Story,
on_delete=models.PROTECT)
date_created = models.DateTimeField(default=timezone.now)
I was able to find a great solution here, overriding the get_url method to map 'pk' value to 'story' directly.
https://stackoverflow.com/a/27584761/7308261
from rest_framework.reverse import reverse
import urllib
class StoryPostsHyperlinkedIdentityField(serializers.HyperlinkedIdentityField):
def get_url(self, obj, view_name, request, format):
lookup_field_value = getattr(obj, self.lookup_field, None)
result = '{}?{}'.format(
reverse(view_name, kwargs={}, request=request, format=format),
urllib.parse.urlencode({'story': lookup_field_value})
)
return result
class StorySerializer(serializers.HyperlinkedModelSerializer):
posts = StoryPostsHyperlinkedIdentityField(
view_name='post-list',
)
class Meta:
model = models.Story
fields = ['url', 'title', 'openings', 'date_created', 'posts']

How to use a nested serialize for serializing and deserializing data? django-rest-framework

I have following serializers:
class UserSerializer(ModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'password')
class ProfileSerializer(ModelSerializer):
user = UserSerializer()
class Meta:
model = Profile
fields = ('id', 'user', 'name', 'address')
when I want to create a profile I should send following data:
{
"user":{
"username": "test_username",
"password": "123456789"
},
"name": "David",
"address": "Baker St"
}
my question is, is it possible to just send "user": 5 instead of sending dictionary in case of POST request?
You have two options, either you can use two different serializers for retrieve and create like this
class ProfileCreateSerializer(ModelSerializer):
class Meta:
model = Profile
fields = ('id', 'user', 'name', 'address')
and
class ProfileRetrieveSerializer(ProfileCreateSerializer):
user = UserSerializer()
and decide which serializer to use in view (hint: override get_serializer_class method)
OR
Use one serialzer and decide field type according to action type:
class ProfileCreateSerializer(ModelSerializer):
class Meta:
model = Profile
fields = ('id', 'user', 'name', 'address')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.context['request'].method == 'GET': # or whatever condition you want to use
self.fields['user'] = UserSerializer()
you can do, that you can create your custom serializer fields
class CustomForeignKeyField(serializers.PrimaryKeyRelatedField):
def get_queryset(self):
return self.queryset
def to_representation(self, value):
value = super().to_representation(value)
user = User.objects.get(pk=value)
return UserSerializer(user).data
in serializer you can use this fiedls
class ProfileSerializer(ModelSerializer):
user = CustomForeignKeyField(queryset=User.objects.all())
class Meta:
model = Profile
fields = ('id', 'user', 'name', 'address')
it will accept value as int and return the response in json

Represent json data with PrimaryKeyRelatedField in django

I'm designing a mailbox with Django. My code is as follows:
#models.py
class Post(models.Model):
text = models.CharField(max_length=256)
sender = models.ForeignKey(User)
receiver = models.ForeignKey(User)
class Comment(models.Model):
post = models.ForeignKey(Post)
text = models.CharField(max_length=256)
#serializers.py
class CommentSerializer(serializers.ModelSerializer):
post = serializers.PrimaryKeyRelatedField()
class Meta:
model = Comment
fields = [
'id',
'text',
'post'
]
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = [
'id',
'text',
'sender',
'receiver',
]
class MainUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['username', 'email']
I tried to customize serializer and have a serializer as follows:
class PostSerializer(serializers.Field):
def to_representation(self, value):
return PostSerializer(value, context={'request': self.context['request']}).data
def to_internal_value(self, id):
try:
id = int(id)
except ValueError:
raise serializers.ValidationError("Id should be int.")
try:
post = Post.objects.get(pk=id)
except User.DoesNotExist:
raise serializers.ValidationError("Such a post does not exist")
return user
I want to represent comment objects like this
{
"post":{
"text" = "Hello"
"sender" = 1
"receiver" = 2
}
"text": "Greate"
}
My code works great but The problem is it doesn't show the Combo Box for selecting the post. I also tried to customize the PrimaryKeyRelatedField's to_represent method in this way:
class PostSerializer(serializers.PrimaryKeyRelatedField):
def to_representation(self, value):
post_id = super(PostSerializer, self).to_representation(value)
post = Post.objects.get(pk=user_id)
return PostSerializer(
user, {"context":self.context['request']}
).data
but it says the unhashable type: 'ReturnDict' and as I understand we could return anything but simple things such as int or string. Is there a way to do this?

Django Rest Framework error: {"user":["This field is required."]

When posting this:
curl -X POST -H "Authorization: Token sometoken" -d "url=someurl" 127.0.0.1:8000/create/
I get the error:
{"user":["This field is required."] with the ItemSerializer,
I have seen other posts on SO talking about using perform_create, which I am trying to use to save the user object, but it doesn´t work for some reason. Perform_create works when user is defined like this:
user = serializers.CharField(
default=serializers.CurrentUserDefault()
)
But I want to use the user object, not only CharField storing the username
Serializers:
class UserDetailsSerializer(serializers.ModelSerializer):
class Meta:
model = UserModel
fields = ('pk', 'username', 'email', 'first_name', 'last_name')
read_only_fields = ('email', )
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = ['cat']
class CommentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Comment
fields = [
'comment',
]
class ItemSerializer(serializers.HyperlinkedModelSerializer):
user = UserDetailsSerializer()
category = CategorySerializer(many=True)
thecomments = CommentSerializer(many=True)
timestamp = serializers.SerializerMethodField('get_mytimestamp')
def get_mytimestamp(self, obj):
return time.mktime(datetime.datetime.now().timetuple())
class Meta:
model = Item
fields = [
"url",
"user",
"timestamp",
"categories",
"thecomments",
]
Model:
class Item(models.Model):
url = models.CharField(max_length=1000)
user = models.ForeignKey('auth.User', unique=False)
timestamp = models.DateTimeField(auto_now_add=True)
url = models.CharField(max_length=1000)
categories = models.ManyToManyField(Category)
View:
class ItemCreateAPIView(generics.CreateAPIView):
serializer_class = ItemSerializer
def perform_create(self, serializer):
serializer.save(user=self.request.user)
Solution:
Serializer to create Item:
class CreateSerializer(serializers.HyperlinkedModelSerializer):
timestamp = serializers.SerializerMethodField('get_mytimestamp')
def get_mytimestamp(self, obj):
return time.mktime(datetime.datetime.now().timetuple())
class Meta:
model = Item
fields = [
"url",
"timestamp",
]
views.py
class ItemCreateAPIView(generics.CreateAPIView):
serializer_class = CreateSerializer
def perform_create(self, serializer):
serializer.save(user=self.request.user)
post:
curl -X POST -H "Authorization: Token sometoken" -d "url='someurl'" 127.0.0.1:8000/createitem/
class ItemSerializer(serializers.HyperlinkedModelSerializer):
user = UserDetailsSerializer()
category = CategorySerializer(many=True)
thecomments = CommentSerializer(many=True)
timestamp = serializers.SerializerMethodField('get_mytimestamp')
def get_mytimestamp(self, obj):
return time.mktime(datetime.datetime.now().timetuple())
class Meta:
model = Item
fields = [
"url",
"user",
"timestamp",
"categories",
"thecomments",
]
extra_kwargs = {'user': {'required': False}}
add extra_kwargs = {'user': {'required': False}} to your serializer Meta
i had the same issue.
added extra_kwargs = {'user': {'required': False}} to serializer Meta class and it worked

How to save a modelSerializer that has relations? - django

I want to save a sent json data to db by django-rest-framework.
the problem is, not saving the relation and returns error.
The bellow snippet is my models:
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name='profile', on_delete=models.CASCADE)
name = models.CharField(max_length=30)
family = models.CharField(max_length=50)
class Klass(models.Model):
title = models.CharField(max_length=50)
description = models.CharField(max_length=500)
teacher = models.ForeignKey(Profile, related_name='teacher', on_delete=models.CASCADE)
I use below serializer for serializing/deserializing the Klass model.
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('pk', 'name', 'family')
class KlassSerializer(serializers.ModelSerializer):
teacher = ProfileSerializer()
class Meta:
model = Klass
fields = ('id', 'title', 'description', 'teacher')
now when I prepare a JSON object and send it to the view, it returns error. the below is the view class:
class KlassView(APIView):
"""for SELECT, INSERT Queries"""
def get(self, request, pk):
# somthing
#csrf_exempt
def post(self,request, pk=None):
"""For Creating A Class"""
serializer = KlassSerializer(data=request.data)
if serializer.is_valid():
teacher = ProfileSerializer(request.data['teacher']['pk'])
serializer.teacher = teacher.data
serializer.save()
return Response({'data': serializer.data})
else:
return Response({'data': serializer.errors})
and the error is:
The .create() method does not support writable nested fields by default.
Write an explicit .create() method for serializer mainp.serializers.KlassSerializer, or set read_only=True on nested serializer fields.
How can I save relation in KlassSerializer in order to save to db?
At first change your serializer like below:
class KlassSerializer(serializers.ModelSerializer):
# teacher = ProfileSerializer() # No need to this!
class Meta:
model = Klass
# fields = ('id', 'title', 'description', 'teacher')
fields = ('id', 'title', 'description') # Omit teacher
Then get profile from requested user and pass it to your serializer:
def post(self,request, pk=None):
"""For Creating A Class"""
serializer = KlassSerializer(data=request.data)
if serializer.is_valid():
teacher = ProfileSerializer(request.data['teacher']['pk'])
serializer.teacher = teacher.data
serializer.save(teacher=request.user.profile) # Retrieve teacher and stroe
return Response({'data': serializer.data})
else:
return Response({'data': serializer.errors})
Just override the create method of ModelSerializer in KlassSerializer.
class KlassSerializer(serializers.ModelSerializer):
teacher = ProfileSerializer()
class Meta:
model = Klass
fields = ('id', 'title', 'description', 'teacher')
def create(self, validated_data):
profile = Profile.objects.filter(pk=validated_data['teacher']['pk'])
if profile:
k = Klass()
k.teacher = profile
...