If the queryset exists, I would like to update the existing instance instead of creating a brand new one.
The way I currently have it raises an error saying unexpected kwargs are being passed if the serializer is trying to update.
Could someone please help me do this?
class APNSDeviceViewSet(DeviceViewSetMixin, ModelViewSet):
queryset = APNSDevice.objects.all()
serializer_class = APNSDeviceSerializer
def perform_create(self, serializer):
queryset = APNSDevice.objects.filter(user=self.request.user)
if queryset.exists():
# update serializer to contain new information
serializer.update(
device_type=self.request.data.get('device_type'),
registration_id=self.request.data.get('registration_id'),
device_id=self.request.data.get('device_id'))
else:
# create a new model instance
serializer.save(
user=self.request.user,
device_type=self.request.data.get('device_type'),
registration_id=self.request.data.get('registration_id'),
device_id=self.request.data.get('device_id'))
Thank you!
Peering into the Django Rest Framework code, specifically serializers.py, it's clear you're providing incorrect parameters for the update() method. Its signature is not like the save() method's signature. It expects as a first argument the model instance, and as a second argument a dictionary containing the data. So in your case it would look something like this:
instance = queryset[0]
serializer.update(instance, {'device_type': self.request.data.get('device_type'), 'registration_id': self.request.data.get('registration_id'), 'device_id': self.request.data.get('device_id')})
All you want is a base queryset that is restricted to the current user.
This is performed by the filters in an elegant and simple way.
On a side note you should not invoke serializer.update by yourself. It's called by the serializer through the serializer.save method. The difference between update et create is that the serializer is instantiated with a Model's instance for updates.
Related
I have the following custom model manager in Django that is meant to count the number of related comments and add them to the objects query set:
class PublicationManager(models.Manager):
def with_counts(self):
return self.annotate(
count_comments=Coalesce(models.Count('comment'), 0)
)
Adding this manager to the model does not automatically add the extra field in DRF. In my API view, I found a way to retrieve the count_comments field by overriding the get function such as:
class PublicationDetails(generics.RetrieveUpdateAPIView):
queryset = Publication.objects.with_counts()
...
def get(self, request, pk):
queryset = self.get_queryset()
serializer = self.serializer_class(queryset.get(id=pk))
data = {**serializer.data}
data['count_comments'] = queryset.get(id=pk).count_comments
return Response(data)
This works for a single instance, but when I try to apply this to a paginated list view using pagination_class, overriding the get method seems to remove pagination functionality (i.e. I get a list of results instead of the usual page object with previous, next, etc.). This leads me to believe I'm doing something wrong: should I be adding the custom manager's extra field to the serializer instead? I'm not sure how to proceed given that I'm using a model serializer. Should I be using a basic serializer?
Update
As it turns out, I was using the model manager all wrong. I didn't understand the idea of table-level functionality when what I really wanted was row-level functionality to count the number of comments related to a single instance. I am now using a custom get_paginated_response method with Comment.objects.filter(publication=publication).count().
Original answer
I ended up solving this problem by creating a custom pagination class and overriding the get_paginated_response method.
class PaginationPublication(pagination.PageNumberPagination):
def get_paginated_response(self, data):
for item in data:
publication = Publication.objects.with_counts().get(id=item['id'])
item['count_comments'] = publication.count_comments
return super().get_paginated_response(data)
Not sure it's the most efficient solution, but it works!
I am using Django detailview. initially, I used the URL pattern
url(r'^todo/details/(?P<pk>[\d]+)', views.todoDetailView.as_view(), name='detail_todo'),
my view is
class todoDetailView(DetailView):
model = models.todo
It worked fine.
In the second case, my URL is
url(r'^todo/details/(?P<id>[\d]+)', views.todoDetailView.as_view(), name='detail_todo'),
this time, I modified my view to
class todoDetailView(DetailView):
model = models.todo
# context_object_name = 'todo_detail'
def get_object(self, **kwargs):
print(kwargs)
return models.todo.objects.get(id=self.kwargs['id'])
It worked fine, I modified the second case to
class todoDetailView(DetailView):
model = models.todo
# context_object_name = 'todo_detail'
def get_queryset(self):
return models.todo.objects.get(id=self.kwargs['id'])
then I get an error,
Generic detail view todoDetailView must be called with either an object pk or a slug.
I know that there is no proper slug or pk provided. So, initially I added get_object() (it worked) but get_queryset() is not working. What is the difference in their working ??
And also if a user is getting details only based on the slug, I read on StackOverflow that
this can be used
slug_field = 'param_name'
slug_url_kwarg = 'param_name'
link - Generic detail view ProfileView must be called with either an object pk or a slug
Can anyone explain me actual working of get_object() and get_queryset() (also get_slug_field() if possible)
Along with the terms slug_field and slug_url_kwarg
Thanks in advance
get_object returns an object (an instance of your model), while get_queryset returns a QuerySet object mapping to a set of potentially multiple instances of your model. In the case of the DetailView (or in fact any class that inherits from the SingleObjectMixin, the purpose of the get_queryset is to restrict the set of objects from which you'll try to fetch your instance.
If you want to show details of an instance, you have to somehow tell Django how to fetch that instance. By default, as the error message indicates, Django calls the get_object method which looks for a pk or slug parameter in the URL. In your first example, where you had pk in the URL, Django managed to fetch your instance automatically, so everything worked fine. In your second example, you overrode the get_object method and manually used the id passed as parameter to fetch the object, which also worked. In the third example, however, you didn't provide a get_object method, so Django executed the default one. SingleObjectMixin's default get_object method didn't find either a pk or a slug, so it failed.
There are multiple ways to fix it:
1. Use pk in the URL
The simplest one is to simply use the code you provided in your first example. I don't know why you were unsatisfied with that, it is perfectly fine. If you're not happy, please explain why in more detail.
2. Override get_object
This is the second solution you provided. It is overkill because if you properly configured your view with the correct options (as you will see in the following alternatives), Django would take care of fetching the object for you.
3. Provide the pk_url_kwarg option
If you really want to use id in the URL for some reason, you can indicate that in your view by specifying the pk_url_kwarg option:
class todoDetailView(DetailView):
model = models.todo
pk_url_kwarg = 'id'
4. Provide the slug_field and slug_url_kwarg options [DON'T DO THIS]
This is a terrible solution because you are not really using a slug, but an id, but it should in theory work. You will basically "fool" Django into using the id field as if it was a slug. I am only mentioning it because you explicitly asked about these options in your question.
class todoDetailView(DetailView):
model = models.todo
slug_field = 'id'
slug_url_kwarg = 'id'
Regarding your get_queryset method: in your example, it doesn't even get to be executed, but in any case it is broken because it returns an individual object instead of a queryset (that's what objects.get does). My guess is you probably don't need a custom get_queryset method at all. This would be useful for example if you had a complex permission system in which different users can only access a different subset of todo objects, which I assume is not your case. Currently, if you provide this get_queryset method, even if everything else is configured properly, you will get an error. Probably an AttributeError saying that the queryset object has no attribute filter (because it will actually be a todo object and not a QuerySet object as Django expects).
The default get_object for DetailView tries to fetch the object using pk or slug from the URL. The simplest thing for you to do is to use (?P<pk>[\d]+) in the URL pattern.
When you override get_object, you are replacing this default behaviour, so you don't get any errors.
When you override get_queryset, Django first runs your get_queryset method an fetches the queryset. It then tries to fetch the object from that queryset using pk or slug, and you get an error because you are not using either of them.
The slug_field and slug_url_kwarg parameters are both defined in the docs. The slug_fields is the name of the field in the model used to fetch the item, and slug_url_kwarg is the name of the parameter in the URL pattern. In your case, you are fetching the object using the primary key (pk/id), so you shouldn't use either of these options.
For your URL pattern with (?P<id>[\d]+), you could use pk_url_kwarg = 'id'. That would tell Django to fetch the object using id from the URL. However, it's much simpler to use your first URL pattern with (?P<pk>[\d]+), then you don't have to override any of the methods/attributes above.
get_object() majorly uses with the generic views which takes pk or id
like: DetailView, UpdateView, DeleteView
where get_queryset() uses with the ListView where we are expecting more objects
Also, get_object() or self.get_object() uses pk as default lookup field or can use slug Field
a little peek at get_object()
if queryset is None:
queryset = self.get_queryset()
# Next, try looking up by primary key.
pk = self.kwargs.get(self.pk_url_kwarg)
slug = self.kwargs.get(self.slug_url_kwarg)
if pk is not None:
queryset = queryset.filter(pk=pk)
# Next, try looking up by slug.
if slug is not None and (pk is None or self.query_pk_and_slug):
slug_field = self.get_slug_field()
queryset = queryset.filter(**{slug_field: slug})
I can't help you with what the error message explicitly means, but get_queryset is used in listviews by get multiple objects, while get_object is used to get a single object (ie DetailView).
If you have a pk you can use to get an object, you don't need to specify a slug field. Slug field are used to filter out objects when you don't have or can't show the primary key publicly. This gives a better explanation of a slug field.
I currently have the following viewset:
class ArtistProfileViewSet(viewsets.ModelViewSet):
queryset = ArtistProfile.objects.all()
serializer_class = ArtistProfileSerializer
def perform_create(self, serializer):
if serializer.is_valid():
serializer.save()
With the following serializers:
class SimpleArtistTrackSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ArtistTrack
fields = (...my fields...)
class ArtistProfileSerializer(serializers.HyperlinkedModelSerializer):
owners = UserSerializer(many=True, required=False)
tracks = SimpleArtistTrackSerializer(many=True, required=False)
class Meta:
model = ArtistProfile
fields = (...my fields...)
I am getting the following error:
AssertionError: You cannot call `.save()` after accessing `serializer.data`.If you need to access data before committing to the database then inspect 'serializer.validated_data' instead.
I don't see where I would be editing the serializer.data object. Is there something I am missing that would cause this edit? How can I resolve this error?
If you are using PyCharm and you were debugging some code, check if you hadn't called serializer.data in your debugger "watches". It call .data everytime your debugger stop in given context(breakpoint) that given serializer appear, even if you don't call it explicit(serializer.data) in your code.
You don't need to call is_valid there at all. When you do an update the serializer is initialized with the model instance (based of the pk in the route, e.g PATCH /artists/{3}/). You would call is_valid() if you are passing data to the serializer as in:
ser = MySerializer(data=request.data) # validate incoming post, etc
ser.is_valid(raise_exceptions=True)
data = ser.validated_data
To send additional data to the serializer when saving (e.g. to set a company, or set a user, etc), use an override like this:
def perform_create(self, serializer):
serializer.save(company=self.request.user.company)
For more details, I would browse the DRF source code and see what it is doing. it's very well written and clear.
Your comment indicates another question which is much bigger. You say you want to add additional data to the "join" table between artists and user. This can be done, but is a whole different topic and you end up manually managing the relationship. Also, read up on nested writable serializers, it's a big topic and even more manual labor.
In the same way that "watches" if you are debugging, try not to inspect (or expand) the 'data' field of the serializer before going through the save() line.
I have Folder model. Folder has foreignkey to User called folders.
In django rest framework I would like to create new Folder object. How do I specify "create object at self.request.user.folders"? under current (out of the box vanilla ModelViewSet) implementation I get error:
folder.folder.user_id may not be NULL
This indicates that a we are trying to create Folder object is without specifying a user.
Is there a built in way or will I need to override the create method and pass user in to the serializer as a argument?
Note that we don't want to pass user_id in request.DATA due to security issues.
models.py:
class FolderModel(Model):
user = ForeignKey(User, related_name='folders')
title = CharField(max_length=100)
views.py:
class FolderView(viewsets.ModelViewSet):
serializer_class = serializers.FoldereSerializer
model = FolderModel
def get_queryset(self):
return self.request.user.folders.all()
The original answer no longer works as of DRF 3.0, as the functions have changed. The relevant doc is here.
From the docs:
The pre_save and post_save hooks no longer exist, but are replaced with perform_create(self, serializer) and perform_update(self, serializer).
These methods should save the object instance by calling serializer.save(), adding in any additional arguments as required. They may also perform any custom pre-save or post-save behavior.
For example:
def perform_create(self, serializer):
# Include the owner attribute directly, rather than from request data.
instance = serializer.save(owner=self.request.user)
# Perform a custom post-save action.
send_email(instance.to_email, instance.message)
fom the docs`:
def pre_save(self, obj):
obj.owner = self.request.user
Here is my view.
class ModelxUpdateView(LoginRequiredMixin, UpdateView):
model = Modelx
template_name='template.html'
form_class = ModelxFormSet
def get_queryset(self):
# query_set = super(ModelxUpdateView, self).get_queryset().filter(user=self.request.user)
query_set = Modelx.objects.filter(user=self.request.user)
return query_set
The error that it's throwing is
AttributeError: Generic detail view ModelxUpdateView must be called with either an >object pk or a slug.
Could someone also clarify if editing multiple models is allowed via UpdateView? I mean if I am going to return a queryset, it's going to update each of the objects in that queryset right?
Could someone also clarify if editing multiple models is allowed via
UpdateView? I mean if I am going to return a queryset, it's going to
update each of the objects in that queryset right?
No, UpdateView is for a single object only. It inherits from the SingleObjectMixin which is why it needs a primary key to be passed in; as this primary key is used in the get_object method.
To do multiple object updates, try the UpdatesWithInlines view from the django-extra-views app.