I am building a BlogApp and I am trying to get the last instance of model in which request.user in ManyToManyField
I have tried using
models.py
class Blog(models.Model):
title = models.CharField(max_length=3000)
likes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='post_likes')
views.py
def get_data(request):
get_last_blog = Blog.objects.filter(likes__in=[request.user]).last()
print(get_last_blog)
But it is showing first instance not last. I have tried without list like likes__in=request.user but it shows
'User' object is not iterable
I have tried many times but it is still not working. I am new in django. Any help would be much Appreciated. Thank You in Advance
.last() gets last or None object from queryset, so it's based on your order_by. I think you should be using .latest('<your_timestamp_field') and it will get latest object based on that timestamp field. Remember that .latest() can raise ObjectDoesNotExist exception if queryset is empty
Related
I have a Django REST API which works perfectly when I want to query some data from the database.
Here I have an example:
views.py
class ProductListAPIView(generics.ListAPIView):
def get_queryset(self):
# Collect data from products table and filter it
queryset = Product.objects.filter(name="Ferrari", date__lte="2022-08-01") # <- Substitute with line below
return queryset
serializer_class = ProductSerializer
authentication_classes = [authentication.SessionAuthentication, authentication.TokenAuthentication]
permission_classes = [IsOwnerPermission]
serializers.py
from rest_framework import serializers
from insert_data.models import Product
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ["name","category","date","price"]
The output is what I expect i.e. a json response with the content of the database.
The problems arise when I try to group the queryset in order to return the average price of a product. By doing some research in the internet I noticed that I just need to substitute the line of code in the views.py script with this:
Product.objects.filter(name=params['name'], date__lte=query.date).values('name','category').annotate(price_average=Avg('price')).order_by()
I am pretty confident that this does the work, however the API returns an error which I do not know how to fix:
AttributeError: 'int' object has no attribute 'pk'
I totally have no idea what this refers to. Would you be able to suggest a smart and elegant way to return the average price of a product after a group by operation via the REST API?
Is category a ForeignKey to a model? I think the problem is with this field in that case.
I believe that when you call .values() you get the id of foreignKey-fields and you can't access that field as an object anymore.
So REST Framework is trying to access the property pk on what it thinks is an instance of Category.
To fix this you can create a custom serializer where category is an integer instead and you've added price_average. You have to check the field types, I don't know what types you have.
class ProductPriceAverageSerializer(serializers.Serializer):
category = serializers.IntegerField()
price_average = serializers.FloatField()
I am building a simple class group app in which I am trying to add particular users from another model's ManyToFieldField to a new model's ManyToFieldField.
class ClassGroup(models.Model):
admins = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='admins')
members = models.ManyToManyField(settings.AITH_USER_MODEL)
title = models.CharField(max_length=9999, default='')
class ClassGroupInvite(models.Model):
class_group = models.ForeignKey(ClassGroup, on_delete=models.CASCADE)
invite_receiver = models.ManyToManyField(class_group.admins.all())
invite_sender = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
As you can see that I am filtering (send request only to class group admins) in ClassGroupInvite with setting ManyToManyField with ClassGroup.admins
But when I try this then it is showing
ManyToManyField(<django.db.models.fields.related_descriptors.ManyToManyDescriptor object at 0x000001CE78793280>) is invalid. First parameter to ManyToManyField must be either a model, a model name, or the string 'self'
I also read the documentation about it, But I didn't find anything about defining it.
then I tried using ClassGroup.admins.all then it showed
AttributeError: 'ManyToManyDescriptor' object has no attribute 'all'
I have tried many times but it is still not working, Any help would be much Appreciated. Thank You in Advance.
When using class-based generic views in Django, having a queryset attribute means to "restrict" the collection of object the view will operate on, right?
If queryset is provided, that queryset will be used as the source of objects. (Django's get_object())
Model:
from django.db import models
class Person(models.Model):
full_name = models.CharField(max_length=30)
is_active = False
View:
from django.views.generic import DetailView
from books.models import Person
class PersonDetail(DetailView):
queryset = Person.objects.filter(is_active=True)
The queryset above makes sure to only consider objects with is_active=true.
But how does this works internally?
For example: Does Django appends a SQL condition AND is_active=TRUE to every query in the view?
Ok that last example seems pretty stupid but I hope you get the idea of my question. Thank you.
Yes, this is exactly what happens. Your queryset is used as the base queryset by the view's get_object method. It then applies an additional filter to get a specific object (e.g., by ID):
queryset = queryset.filter(pk=pk)
Of course, the view needs a single object, not a queryset, so it then does:
obj = queryset.get()
Which will either return a single object, or a DoesNotExist or MultipleObjectsReturned exception. DoesNotExist results in a 404. MultipleObjectsReturned is unhandled and will propagate to your code.
I need to group and count events by day in the django admin.
Here's my current admin queryset:
class TransactionLogAdmin(ExportMixin, OrganizationRestrictedAdmin):
list_display = ['get_type_count', 'get_count']
def get_queryset(self, request):
return TransactionLog.objects.all().extra(
select={'day': 'date( date_created )'}).values('datetime')
But I'm getting the following error:
'dict' object has no attribute '_meta'
This is the problem
return TransactionLog.objects.all().extra(
select={'day': 'date( date_created )'}).values('datetime')
you have get_queryset method in the admin. As the name suggests, that method is expected to return a queryset. But you are returning a dictionary here. Remove the call to values()
When over riding get_queryset, it's always a good idea to call the method in the superclass and make modifications to it instead of making a new queryset of your own.
Hi I have User object imported from auth and different model named UserProfile which has bio of user
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
bio=models.TextField(null=True,blank=True, max_length=128)
def __unicode__(self):
return self.user
as the user is OneToOneField Im having inserting data into it
q=User.objects.get(id=1)
>>> <User: test>
q.userprofile_set.create(bio='check')
Im getting error 'User' object has no attribute 'userprofile_set'. Im new to django..How should I insert data which has a OneToOneField
Any help is much appreciated...Thnaks in advance
ManyToManyFields and Foreignkey fields create a object.relation_set property on the object you can follow, but OneToOneFields do not. Instead you use object.relation directly (since we know there's only one).
So try q.userprofile.create(bio="check") instead!
(Bonus tip: Whenever you want to see the properties of a python object, use the dir() method, it will show you the full list of properties available!)