Django Queryset with custom property - django

django==3.0.5 django-filter==2.3.0 djongo==1.3.3 my database is mongodb
I have a simple model like below
class Parishioner(models.Model):
def _age(self):
return date.today().year - self.dob.year
"""Parishioner model"""
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
dob = models.DateField()
age = property(_age)
GENDER_CHOICES = [("Male", "Male"), ("Female", "Female"), ("Other", "Other")]
gender = models.CharField(max_length=10, choices=GENDER_CHOICES)
address = models.CharField(max_length=1000)
fathers_name = models.CharField(max_length=500)
mothers_name = models.CharField(max_length=500)
baptism_certificate = models.ImageField(null=True, upload_to=baptism_certificates_image_file_path)
marriage_certificate = models.ImageField(null=True, upload_to=marriage_certificates_image_file_path)
As you can see age is a calculated property.
I have my viewset like below
class ParishionerViewSet(viewsets.ModelViewSet):
queryset = Parishioner.objects.all()
serializer_class = serializers.ParishionerSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
def get_queryset(self):
first_name = self.request.query_params.get('first_name')
last_name = self.request.query_params.get('last_name')
gender = self.request.query_params.get('gender')
address = self.request.query_params.get('address')
fathers_name = self.request.query_params.get('fathers_name')
mothers_name = self.request.query_params.get('mothers_name')
age = int(self.request.query_params.get('age'))
queryset = self.queryset
if first_name:
queryset = queryset.filter(first_name__contains=first_name)
if last_name:
queryset = queryset.filter(last_name__contains=last_name)
if gender:
queryset = queryset.filter(gender__contains=gender)
if address:
queryset = queryset.filter(address__contains=address)
if fathers_name:
queryset = queryset.filter(fathers_name__contains=fathers_name)
if mothers_name:
queryset = queryset.filter(mothers_name__contains=mothers_name)
if age:
queryset = queryset.filter(age__gte=age)
return queryset.filter()
All I want is to return obects who has age >= provides value.
So if I send a request to this url http://localhost:8000/api/parishioners/?age=32
I'm getting this error --> Cannot resolve keyword 'age' into field
So how can I use this url http://localhost:8000/api/parishioners/?age=32
and get objects who has age greater than or equal to 32 ?

You cannot use a function / property of the model in a query. You need to bring the logic of the function into the query instead.
from django.db.models.functions import ExtractYear, Now
queryset = queryset.annotate(age=ExtractYear(Now()) - ExtractYear('dob')).filter(age__gte=age)

Related

Django query set to display particular field on webpage

I am trying to display the variants(field) that are related to the model RecordVariant on my webpage i have queryset on my view default how can i change my queryset or get queryset method to display variants that are related to particular record.
class RecordVariant(models.Model):
variants = models.ForeignKey(Variant_model, related_name = 'records', on_delete =
models.CASCADE)
class Variant(models.Model):
name = models.CharField(max_length = 23, blank=True, null=True)
class RecordVariantListAPIView(RecordMixin, ListAPIView):
lookup_url_kwarg = 'record_id'
serializer_class = RecordVariantSerializer
permission_classes = [IsAuthenticated]
pagination_class = StandardResultsSetPagination
filter_backends = (filters.OrderingFilter,)
queryset = RecordVariant.objects.all()
ordering = 'variants'
ordering_param = 'ordering'
ordering_fields = (
'variants',
)
def get_total_queryset(self):
queryset = (
super()
.get_queryset()
.filter(record=self.record)
)
in foreignkey relations the child should be the one with foreginkey field.
meaning your Variant model class should have this field
class Variant(models.Model):
name = models.CharField(max_length = 23, blank=True, null=True)
records = models.ForeignKey(RecordVariant, related_name = 'records',
on_delete = models.CASCADE)
class RecordVariant(models.Model):
pass
and for the serializers
class Variant(serializers.ModelSerializer):
class Meta:
model = Variant
fields = "['name']"
class RecordVariant(serializers.ModelSerializer):
records = VariantSerializer(many=True)
class Meta:
model = RecordVariant
fields = '__all__'
the view
class RecordVariantListAPIView(ListAPIView):
serializer_class = RecordVariantSerializer
permission_classes = [IsAuthenticated]
queryset = RecordVariant.objects.all()

Filter by fields from foreignKey relationships

I got a bunch of models and some of them are connected (by foreign-key relationships) and I wrote a serializer which allows me to print out all of the connected fields that I want, and leave out what I do not want to see. Great. Now I also have a basic filter, which uses the model (PmP) which contains all the foreignkeys, but now I want to add another filter for a field (field name e from PmPr Model) from a different Model, one that is read in via foreignkey connection (li in Model PmP connects to model PmL containing field pro which connects to model PmPr where the field e is). But I dont know how to do that and as far as I can see, I cant set two filter_classes inside my view (PmPLListView)?! And I dont know how to access the field via the foreignkey relation. So how do I go about this? If I can access the e field from PmPr Model via my existing filter - than that is also fine with me, I dont necessary want two filter classes (if even possible). It was just me first thought. (btw. sorry about the strange names, but unfortunately I'm not allowed to write the real names)
these are my models (at least the relevant ones):
class PmP(models.Model):
created_at = models.DateTimeField()
pr = models.DecimalField(max_digits=6, decimal_places=2)
li = models.ForeignKey(PmL, models.DO_NOTHING)
se = models.ForeignKey('PmSe', models.DO_NOTHING)
class Meta:
managed = False
db_table = 'pm_p'
class PmL(models.Model):
u = models.TextField()
pro = models.ForeignKey('PmPr', models.DO_NOTHING)
sh = models.ForeignKey('PmS', models.DO_NOTHING)
active = models.IntegerField()
class Meta:
managed = False
db_table = 'pm_l'
class PmSe(models.Model):
name = models.TextField()
s_i_id = models.TextField(blank=True, null=True)
sh = models.ForeignKey('PmS',
models.DO_NOTHING,
blank=True,
null=True)
class Meta:
managed = False
db_table = 'pm_se'
class PmPr(models.Model):
name = models.TextField()
e = models.CharField(max_length=13)
created_at = models.DateTimeField()
cus = models.ForeignKey(PmC, models.DO_NOTHING)
u_v_p = models.DecimalField(max_digits=10,
decimal_places=2,
blank=True,
null=True)
cf = models.IntegerField(blank=True, null=True)
s_k_u = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'pm_pr'
this is what my serializer looks like:
class PmPLSerializer(serializers.ModelSerializer):
# id = serializers.SerializerMethodField('get_l_id')
u = serializers.SerializerMethodField('get_l_u')
sh = serializers.SerializerMethodField('get_sh_name')
name = serializers.SerializerMethodField('get_pro_name')
e = serializers.SerializerMethodField('get_pro_e')
u_v_p = serializers.SerializerMethodField('get_pro_u_v_p')
s_k_u = serializers.SerializerMethodField('get_pro_s_k_u')
se = serializers.SerializerMethodField('get_se_name')
pr = serializers.SerializerMethodField('get_pr')
created_at = serializers.SerializerMethodField('get_created_at')
class Meta:
model = PmP
# fields = '__all__'
fields = ('u', 'sh', 'name', 'e', 's_k_u', 'u_v_p', 'pr',
'created_at', 'se')
depth = 2
def get_l_id(self, obj):
return obj.li.id
def get_l_u(self, obj):
return obj.li.u
def get_sh_name(self, obj):
return obj.li.sh.name
def get_pro_name(self, obj):
return obj.li.pro.name
def get_pro_e(self, obj):
return obj.li.pro.e
def get_pro_u_v_p(self, obj):
return obj.li.pro.u_v_p
def get_pro_s_k_u(self, obj):
return obj.li.pro.s_k_u
def get_se_name(self, obj):
return obj.se.name
def get_pr(self, obj):
return obj.pr
def get_created_at(self, obj):
return obj.created_at
this is my filter class:
class PmPFilter(rfilters.FilterSet):
class Meta:
model = PmP
fields = [
"created_at",
"pr",
]
for field in ["pr"]:
exec(f'min_{field} = rfilters.NumberFilter(field, lookup_expr="gte")')
exec(f'max_{field} = rfilters.NumberFilter(field, lookup_expr="lte")')
# filter by date as "is_less_than_or_equal_to"
written_to = rfilters.CharFilter(method="created_at_to", label="created_at to")
# filter by date as "is_greater_than_or_equal_to"
written_from = rfilters.CharFilter(method="created_at_from", label="created_at from")
# filter by exact date
written = rfilters.CharFilter(method="created_at_exact", label="created_at exact")
def created_at_exact(self, queryset, name, value):
year, month, day, hour, minute, second = self.parse_date(value)
cdate = datetime(year, month, day, hour, minute, second)
return queryset.filter(created_at=cdate)
def created_at_to(self, queryset, name, value):
year, month, day, hour, minute, second = self.parse_date(value)
cdate = datetime(year, month, day, hour, minute, second)
return queryset.filter(created_at__lte=cdate)
def created_at_from(self, queryset, name, value):
year, month, day, hour, minute, second = self.parse_date(value)
cdate = datetime(year, month, day, hour, minute, second)
return queryset.filter(created_at__gte=cdate)
def parse_date(self, value):
return (
parser.parse(value).year,
parser.parse(value).month,
parser.parse(value).day,
parser.parse(value).hour,
parser.parse(value).minute,
parser.parse(value).second,
)
and finally, this is my view:
class PmPLListView(generics.ListAPIView):
queryset = PmP.objects.all()
serializer_class = PmPLSerializer
filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
ordering_fields = ["created_at", "pr"]
filter_class = PmPFilter
fields = ("created_at", "pr")
filter_fields = fields
search_fields = fields
def get_queryset(self):
"""
This view should return a list of all data
"""
return PmP.objects.filter()
oh I got it! I can access the foreign relationship with two underscores. So I modified my Filter class to this:
class PmPFilter(rfilters.FilterSet):
class Meta:
model = PmPrice
fields = [
"created_at",
"pr",
"li__pro__e",
]
...
and inside my PmPLListView view I also added the double underscores to access the field:
class PmPLListView(generics.ListAPIView):
queryset = PmP.objects.all()
serializer_class = PmPLSerializer
filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
ordering_fields = ["created_at", "pr"]
filter_class = PmPFilter
fields = ("created_at", "pr", "li__pro__e")
filter_fields = fields
search_fields = fields
now I can filter by field e

KeyError: 'id' in django rest framework when trying to use update_or_create() method

I am trying to update the OrderItem model using update_or_create() method. OrderItem model is related to the Order model with many to one relationship ie with a Foreignkey.
I am trying to query the orderitem object using id and update the related fields using default as you can see, but got this error.
My models:
class Order(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True)
ordered_date = models.DateTimeField(auto_now_add=True)
ordered = models.BooleanField(default=False)
total_price = models.CharField(max_length=50,blank=True,null=True)
#billing_details = models.OneToOneField('BillingDetails',on_delete=models.CASCADE,null=True,blank=True,related_name="order")
def __str__(self):
return self.user.email
class Meta:
verbose_name_plural = "Orders"
ordering = ('-id',)
class OrderItem(models.Model):
#user = models.ForeignKey(User,on_delete=models.CASCADE, blank=True)
order = models.ForeignKey(Order,on_delete=models.CASCADE, blank=True,null=True,related_name='order_items')
item = models.ForeignKey(Product, on_delete=models.CASCADE,blank=True, null=True)
order_variants = models.ForeignKey(Variants,on_delete=models.CASCADE,blank=True,null=True)
quantity = models.IntegerField(default=1)
ORDER_STATUS = (
('To_Ship', 'To Ship',),
('Shipped', 'Shipped',),
('Delivered', 'Delivered',),
('Cancelled', 'Cancelled',),
)
order_item_status = models.CharField(max_length=50,choices=ORDER_STATUS,default='To_Ship')
My view:
class UpdateOrderView(UpdateAPIView):
permission_classes = [AllowAny]
queryset = Order.objects.all()
serializer_class = OrderUpdateSerializer
My serializers:
class OrderUpdateSerializer(serializers.ModelSerializer):
order_items = OrderItemUpdateSerializer(many=True)
billing_details = BillingDetailsSerializer()
class Meta:
model = Order
fields = ['id','ordered','order_status','order_items','billing_details']
def update(self, instance, validated_data):
instance.order_status = validated_data.get('order_status')
instance.ordered = validated_data.get('ordered')
#billing_details_logic
billing_details_data = validated_data.pop('billing_details',None)
if billing_details_data is not None:
instance.billing_details.address = billing_details_data['address']
instance.billing_details.save()
#order_items_logic
instance.save()
order_items_data = validated_data.pop('order_items')
# print(order_items_data)
#instance.order_items.clear()
for order_items_data in order_items_data:
oi, created = OrderItem.objects.update_or_create(
id= order_items_data['id'],
defaults={
'quantity' : order_items_data['quantity'],
'order_item_status': order_items_data['order_item_status']
}
)
super().update(instance,validated_data)
return oi
Updated serializer:
for order_item_data in order_items_data:
oi, created = instance.order_items.update_or_create(
id= order_item_data['id'],
defaults={
'quantity' : order_item_data['quantity'],
'order_item_status': order_item_data['order_item_status']
}
)
The order_items data are sent like this.
order_items_data is a list.
Then you iterate over it with the same variable name.
for order_items_data in order_items_data:
Just rename it to something like
for order_data in order_items_data:
and there will be an id in your order_data.
I wasn't getting the id on the OrderedDict also, so I've added a id = serializers.IntegerField(required=False) on the serializer and put that id into the update_or_create method:
for obj in data:
Model.objects.update_or_create(
pk=obj.get('id', None), ...
)

Django custom queryset returns nothing

I am trying to write a year level filter for my student profile list, however, the query returns an empty [].
This is my Attendance model, manager and custom queryset:
class AttendanceQuerySet(models.QuerySet):
def get_yearlevel(self, yearlevel):
return self.filter(BCEID__YearLevel = yearlevel)
class AttendanceManager(models.Manager):
def get_queryset(self):
return AttendanceQuerySet(self.model, using=self._db)
def get_yearlevel(self, yearlevel):
return self.get_queryset().get_yearlevel(yearlevel)
class Attendance(models.Model):
BCEID = models.OneToOneField(StudentProfile,primary_key=True,on_delete=models.CASCADE)
AttendanceRate = models.CharField(max_length=10)
objects = AttendanceManager()
def __unicode__(self):
return self.BCEID
StudentProfile model:
class StudentProfile(models.Model):
RelatedPersonName = models.CharField(max_length=10)
RelatedPersonFirstName = models.CharField(max_length=30)
RelatedPersonFamName = models.CharField(max_length=30)
StudentLegalName = models.CharField(max_length=30)
StudentFamName = models.CharField(max_length=30)
Email = models.CharField(max_length=130)
Street1 = models.TextField(max_length=30)
Suburb = models.CharField(max_length=30)
State = models.CharField(max_length=5)
PostCode = models.CharField(max_length=6)
StudentLegalName = models.CharField(max_length=30)
StudentFamName = models.CharField(max_length=30)
StudentNo = models.CharField(primary_key=True,max_length=10)
Class = models.CharField(max_length=6)
YearLevel = models.CharField(max_length=10)
objects = StudentProfileManager()
def __unicode__(self):
return self.StudentNo
and AttendanceListView (views.py)
class AttendanceListView(ListView):
model = Attendance
queryset = Attendance.objects.get_yearlevel("Year 8")
I manually queried the database to check if there were errors in my code, and got the same result: an empty array [].
SQL:
SELECT "student_attendance"."BCEID_id",
"student_attendance"."AttendanceRate"
FROM "student_attendance"
INNER JOIN "student_studentprofile"
ON ("student_attendance"."BCEID_id" = "student_studentprofile"."StudentNo")
WHERE "student_studentprofile"."YearLevel" = 'Year 8'
Please let me know what I am doing wrong here.

Count the number of rows in a Django database : AttributeError

I have database created by Django model, where idRecruteur is the fields of the table Offre.
I need to count the number of rows where idRecruteur = 1.
This is my "Offre" table code:
#python_2_unicode_compatible
class Offre(models.Model):
titre = models.CharField(max_length=100)
dateAjout = models.DateField(auto_now=False, auto_now_add=False)
nature = models.CharField(max_length=50)
duree = models.CharField(max_length=50)
niveau = models.CharField(max_length=50)
description = models.CharField(max_length=50)
salaire = models.FloatField(null=True, blank=True)
idRecruteur = models.ForeignKey(Recruteur, related_name="Offre", on_delete=models.CASCADE)
def __str__(self):
return "Offre: {}".format(self.title)
and this is my queryset:
class OffresparEntrepriseViewSet(ModelViewSet):
queryset = Offre.objects.filter(idRecruteur=1).count()
serializer_class = OffreSerializer
I get the error " AttributeError: 'int' object has no attribute 'model' "
Any ideas what I am doing wrong?
ViewSet's queryset attribute should be QuerySet object, but count() returns integer. You need to use queryset = Offre.objects.filter(idRecruteur=1) as viewset queryset, and move counting functionality to another level. For example using list_route:
class OffresparEntrepriseViewSet(ModelViewSet):
queryset = Offre.objects.filter(idRecruteur=1)
serializer_class = OffreSerializer
#list_route()
def count(self, request):
count = self.queryset.count()
return Response({'count': count})
Now you can get count by accesing viewset_url/count url.