How to handle foreign keys with Django Rest Framework - python

I'm struggling to make my API work, the tutorials are quite tricky about this part. I want to have a '/comments/' POST request with body {movie_id: 1, content="Some comment") and connect it to some Movie.
In serializer I'm getting:
{'movie': [ErrorDetail(string='This field is required.', code='required')]}
How can I map movie_id to movie? By the way, I can change the name to movie if this would be easier.
Models.py:
from django.db import models
from django.utils import timezone
class Movie(models.Model):
title = models.CharField(max_length=200)
year = models.IntegerField()
class Comment(models.Model):
content = models.TextField(max_length=300)
publish_date = models.DateField(default=timezone.now())
movie = models.ForeignKey(Movie, on_delete=models.CASCADE, related_name='movie_id')
serializers.py:
class MovieSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Movie
fields = '__all__'
class CommentSerializer(serializers.HyperlinkedModelSerializer):
movie_id = serializers.PrimaryKeyRelatedField(many=False, read_only=True)
class Meta:
model = Comment
fields = '__all__'
views.py (for Comment, Movie works fine):
from .models import Movie, Comment
from rest_framework import viewsets, status
from rest_framework.response import Response
from .serializers import MovieSerializer, CommentSerializer
class CommentViewSet(viewsets.ModelViewSet):
queryset = Comment.objects.all()
serializer_class = CommentSerializer
def create(self, request, *args, **kwargs):
serializer = CommentSerializer(data=request.data, context={'request': request})
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

I think you can try like this:
class CommentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Comment
fields = '__all__'
Also, related name is used for reverse relationship. So it will work like this:
If Comment Model has related_name comments like this:
class Comment(models.Model):
movie = models.ForeignKey(Movie, on_delete=models.CASCADE, related_name='comments')
Then you can access comments from movie like this:
for m in Movie.objects.all():
m.comments.all()

Nested data works a little differently to how I expected.
If you want to connect a comment to a movie, you need to pass the movie object to your comment, not the primary key of the movie object.
Under the hood, Django automatically creates a new field 'movie_id' on your comment object in which the movie's primary key is stored - but you don't need to worry about that. So I would call the field in the comment 'movie', otherwise Django will create a new field 'movie_id_id'.
I got something similar to work by defining a custom create method in my serializer:
In your serializer:
class CommentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Comment
fields = '__all__'
def create(self, validated_data):
themovieid = validated_data.pop('movie_id', None) # remove movie_id from the comment data
themovie = Movie.objects.get(pk=themovieid) # find the movie object
return Comment.objects.create(movie=themovie, **validated_data)
I have tried to adapt this to your code, I hope it will help you to get this working. I have removed movie_id from your serializer: your model defiines everything that is needed.
Edit: have you tried simply passing the movie's id as 'movie' in your comment data, with no custom create method and do not define 'movie_id' in your serializer?

Related

Is there a way to grab specific "fields" from request.data sent to the Django REST framework API in a POST method

I've got a Project model, with a project_code field. When the API receives a POST request, the request.data will also contain a project_code. I then want to filter my Project model objects based on the project_code inside the request.data
Once I've linked to request.data project_code to the Project model's project_code field, I want to save my Ticket model object to the database. Inside my Ticket model, there is a field called project which is related with a ForeignKey to the Project model.
Thus in essence the project_code inside the POST request.data needs to be used in order to save my Ticket model to the database with the correct Project model foreign Key.
Here are my models:
from django.db import models
class Project(models.Model):
project_code = models.TextField(blank=True)
project_description = models.TextField(blank=True)
def __str__(self):
return self.project_code
class Ticket(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE)
ticket_url = models.TextField(blank=True)
time_submitted = models.DateField(blank=True, auto_now_add=True)
description = models.TextField(blank=True)
user = models.TextField(blank=True)
type = models.TextField(blank=True)
def __str__(self):
return self.description
Here are my serializers:
from rest_framework import serializers
from ticketing_app_api.models import Ticket, Project
class TicketSerializer(serializers.ModelSerializer):
class Meta:
model = Ticket
fields = ['id', 'ticket_url', 'description', 'user', 'type']
And here are my views:
from ticketing_app_api.models import Ticket
from ticketing_app_api.serializers import TicketSerializer
from rest_framework import generics
from rest_framework.decorators import api_view
from rest_framework.response import Response
# from rest_framework.reverse import reverse
from rest_framework import status
#api_view(['GET', 'POST'])
def ticket_list(request):
"""
List all tickets, or creates a new ticket.
"""
if request.method == 'GET':
tickets = Ticket.objects.all()
serializer = TicketSerializer(tickets, many=True)
return Response(serializer.data)
elif request.method == 'POST':
serializer = TicketSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class TicketDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Ticket.objects.all()
serializer_class = TicketSerializer
The cleaner approach would be to get the project_id when you create the project, and then just send it when creating the ticket. In this case, your TicketSerializer must also contain the project:
class TicketSerializer(serializers.ModelSerializer):
class Meta:
model = Ticket
fields = ["id", "ticket_url", "description", "user", "type", "project"]
and when you send the post request, you have to specify which is the project:
{
"ticket_url": "http://ticket.com",
"project": 1
}
In case you must use the project_code, you can set the project when validating the data:
class TicketSerializer(serializers.ModelSerializer):
class Meta:
model = Ticket
fields = ["id", "ticket_url", "description", "user", "type"]
def validate(self, attrs):
attrs["project"] = Project.objects.get(
project_code=self.initial_data.get("project_code")
)
return attrs

How to use a Many-to-Many through model in DRF

So I'm trying to achieve the general "Like" functionality in a social media website using Django and REST Framework, and a frontend in React.
Using a Post model to save all the posts, and I have a Many-to-Many field for storing the likes and created a through model as follows:
class PostLike(models.Model):
user = models.ForeignKey(AppUser, on_delete=models.CASCADE)
post = models.ForeignKey("Post", on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now_add=True)
class Post(models.Model):
user = models.ForeignKey(AppUser, on_delete=models.CASCADE)
caption = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
edited_at = models.DateTimeField(auto_now=True)
likes = models.ManyToManyField(
AppUser, related_name="post_user", blank=True, through=PostLike
)
(AppUser is a custom auth model used)
Similarly, I have created serializers and viewsets for the above models:
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = "__all__"
class PostLikeSerializer(serializers.ModelSerializer):
class Meta:
model = PostLike
fields = "__all__"
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
class PostLikeViewSet(viewsets.ModelViewSet):
queryset = PostLike.objects.all()
serializer_class = PostLikeSerializer
My question is, how do I "like" or remove an existing "like" from a post using API calls?
One method I know is to just make a POST request to the PostLike endpoint using the user PK and the post PK to create a PostLike instance, but I don't know a way to "remove" a like using the same method.
Please help!
you can use APIView instead of ViewSet like this:
from rest_framework import views
class PostLikeApiView(views.APIView):
serializer = PostLikeSerializer(data=request.data)
if serializer.is_valid():
user = serializer.data['user']
post = serializer.data['post']
post_like_obj = PostLike.objects.filter(user=user, post=post)
if post_like_obj.exists():
post_like_obj.delete()
result = 'unliked'
else:
PostLike.objects.create(user=user, post=post)
result = 'liked'
return Response(
{
'result': result,
},
status=status.HTTP_200_OK
)
else:
return Response(
serializer.errors,
status=status.HTTP_400_BAD_REQUEST
)

Django Form: Only show manytomany objects from logged in user

The current problem is that my form shows the logged in user all Portfolios ever created. The form should only show portfolios that the logged-in user created.
Something like this:
associated_portfolios manytomany field = ...objects.filter(user=user_id)
I'm not sure if this should be implemented in the forms.py or views.py and if so how. I've been going through the django documentation and found 'formfield_for_manytomany' but not sure if this is only meant for admin.
Models.py
class Portfolio(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=20)
description = models.CharField(max_length=250, blank=True, null=True)
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=50)
body = RichTextUploadingField(blank=True, null=True)
associated_portfolios = models.ManyToManyField(Portfolio, blank=True)
created_on = models.DateField(auto_now_add=True, editable=False)
Views.py
class PostCreate(CreateView):
model = Post
form_class = PostCreateForm
def formfield_for_manytomany(self, db_field, request, **kwargs):
self.fields['associated_portfolios'] = Portfolio.objects.filter(user=self.request.user)
return super().formfield_for_manytomany(db_field, request, using=self.using, **kwargs)
forms.py
class PortfolioCreateForm(ModelForm):
class Meta:
model = Portfolio
fields = ['user', 'name', 'description']
class PostCreateForm(ModelForm):
class Meta:
model = Post
fields = ['user', 'title', 'body', 'category', 'associated_portfolios']
Since you're using a ModelForm, the associated_protfolios field will be a ModelMultipleChoiceField [docs]. This field has a queryset attribute [docs]. We want to modify that attribute.
Django's CreateView has a method get_form, which in this case will grab your PostCreateForm. This is a good spot to filter the field's queryset, since we have access to the user:
class PostCreate(CreateView):
model = Post
form_class = PostCreateForm
def get_form(self, *args, **kwargs):
form = super().get_form(*args, **kwargs) # Get the form as usual
user = self.request.user
form.fileds['associated_portfolios'].queryset = Portfolio.objects.filter(user=user)
return form
Did you try this
self.fields['associated_portfolios'] = Post.objects.filter(associated_portfolios__portfolio__user=request.user)
OR
user_posts = Post.objects.filter(user=request.user)
self.fields['associated_portfolios'] = user_posts.associated_portfolios.all()
read more about M2M relationships querying here, because I think your problem may be with it.
Also, I'm not sure about your actual data maybe it's right and it gives a correct result as filtering Portfolio model against current user to get its objects looks right for me, but anyway double check everything again.
And as a final note, add related_name to your model fields so you can use it easily for reverse relations rather than going with Django's default naming, it will be clearer and give a better understanding.

500 error: null value in column "timeline_id" violates not-null constraint

I'm currently a student and am trying to create a full stack web application with python/django and React. While building my back-end I have run into a problem where, when posting an object containing an association, the association's id is lost between the response payload and the database. I know that the state is updating as it should and that other objects that don't use associations on the back-end can be created without a problem. I assume my mistake has to be somewhere in the models or serializers that I made, but so far I haven't found it.
500 error message:
Integrity error at "api/v1/event"
null value in column "timeline_id" violates not-null constraint
DETAIL: failing row contains (index_id, junk_data, junk_data, 1, null)
my models:
from django.db import models
class Timeline(models.Model):
name = models.CharField(max_length=50, default='n/a')
def __str__(self):
return self.name
class Event(models.Model):
name = models.CharField(max_length=25, default='n/a')
description = models.CharField(max_length=150, default='n/a')
coordinate = models.IntegerField(default=0)
timeline = models.ForeignKey(Timeline, on_delete=models.CASCADE, related_name="events")
def __str__(self):
return self.name
class Note(models.Model):
article = models.TextField(default='n/a')
event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='notes')
# event = models.ManyToManyField(Event)
def __str__(self):
return self.article
my views:
from rest_framework import viewsets
from .serializers import TimelineSerializer, EventSerializer, NoteSerializer
from .models import Timeline, Event, Note
class TimelineView(viewsets.ModelViewSet):
queryset = Timeline.objects.all()
serializer_class = TimelineSerializer
class EventView(viewsets.ModelViewSet):
queryset = Event.objects.all()
serializer_class = EventSerializer
class NoteView(viewsets.ModelViewSet):
queryset = Note.objects.all()
serializer_class = NoteSerializer
my serializers:
from rest_framework import serializers
from .models import Timeline, Note, Event
class NoteSerializer(serializers.ModelSerializer):
class Meta:
model = Note
fields = ('id', 'title', 'article')
class EventSerializer(serializers.ModelSerializer):
notes = NoteSerializer(many=True, read_only=True)
class Meta:
model = Event
fields = ('id', 'name', 'description', 'coordinate', 'notes')
class TimelineSerializer(serializers.ModelSerializer):
events = EventSerializer(many=True, read_only=True)
class Meta:
model = Timeline
fields = ('id', 'name', 'events')
Link to the github repo:
https://github.com/dantehollo/world_builder_site
I am using python 3.6.8 on linux (ubuntu 18.04.3)
I tried to be as specific and brief as possible. If there is something missing let me know and I will post it as soon as I see the request. Any help with what this is, why it happened and how to avoid it again is greatly appreciated, thank you.
According to your Event model timeline attribute (which is also a foreign key) is required and can not be null. And you forgot to add this timeline into fields of EventSerializer:
class EventSerializer(serializers.ModelSerializer):
notes = NoteSerializer(many=True, read_only=True)
class Meta:
model = Event
fields = ('id', 'name', 'description', 'coordinate', 'notes', 'timeline')
Now if you do not send timeline in the request, DRF will give ValidationError that you have to include it.

Django REST POST with PK to HyperlinkedModelSerializer, how to translate PK to URL?

I'm new to Django and Django REST. I have been creating a simple practice project.
Models:
class Locker(models.Model):
locker_owner = models.ForeignKey(User, related_name='lockers', on_delete=models.CASCADE)
locker_desc = models.CharField(max_length=100)
locker_rating = models.IntegerField(default=0)
Serializers:
class LockerSerializer(serializers.ModelSerializer):
locker_owner = serializers.HyperlinkedRelatedField(
view_name='user-detail',
lookup_field='id',
queryset=User.objects.all())
# how to add locker owner with id?
class Meta:
model = Locker
fields = ('url', 'locker_desc', 'locker_rating', 'locker_owner')
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'id', 'username', 'email', 'groups')
Views:
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
class LockerViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows lockers to be viewed or edited.
"""
# def create(self, request):
queryset = Locker.objects.all()
serializer_class = LockerSerializer
I use this to POST:
ttp -f POST localhost:8000/lockers/ locker_owner_id=2 locker_desc='desc of locker 3' locker_rating=150
But then the response is
"locker_owner": [
"This field is required."
]
Main Point:
I would like to create a new locker, and use User id as its locker_owner, yet still maintain the HyperlinkedModelSerializer. It won't let me, since they need url for locker_owner. Once I use hyperlink in the locker_owner of my POST request it works, but I don't know how to translate locker_owner=2 to it's url.
Any help would be appreciated. I have been looking for answers in days. Thank you!
This is not something you can do out-of-the-box. You will need to implement your own custom serializer Field, and override the to_representation method:
serializers.py
from rest_framework.reverse import reverse
class CustomRelatedField(serializers.PrimaryKeyRelatedField):
def to_representation(self, value):
return reverse('<your-user-detail-view-name>', args=(value.pk,), request=self.context['request'])
class LockerSerializer(serializers.ModelSerializer):
locker_owner = CustomRelatedField(queryset=User.objects.all())
class Meta:
model = Locker
fields = ('url', 'locker_desc', 'locker_rating', 'locker_owner')
You can then simply POST a simple JSON to create a new Locker object:
{
"locker_owner": 2,
"locker_desc": 'desc of locker 3',
"locker_rating": 150
}
I have found the solution myself looking at the tutorials available. The other answers have not really answered the question fully.
For those wanting to get url of a foreign key to be saved, here, locker_owner, just get them using hyperlinked related field
In my serializer:
class LockerSerializer(serializers.HyperlinkedModelSerializer):
locker_owner=serializers.HyperlinkedRelatedField(
read_only=True,
view_name='user-detail')
class Meta:
model = Locker
fields = ('locker_desc', 'locker_rating', 'locker_owner')
I was trying to get the id in the lookup field, but actually the lookup field is searching the id from my url. That's why it could not find it. Simple and done. Thank you for all the answers.

Categories