Reputation: 2730
I have a model called deployed contracts model which looks like following
from django.db import models
class DeployedContracts(models.Model):
house_no = models.CharField(max_length=200, blank=False, default='')
rent = models.IntegerField(blank=False)
security_deposit = models.IntegerField(default=0, blank=False)
house_owner = models.CharField(max_length=100, blank=False, default='')
house_tenant = models.CharField(max_length=100, blank=False, default='')
deployed_contract_address = models.CharField(max_length=100, blank='', default='')
def save(self, *args, **kwargs):
super(DeployedContracts, self).save(*args, **kwargs)
where house_owner and house_tenant contains the email of house owner and house tenant and it's serializer looks like following
from rest_framework import serializers
from deployedcontracts.models import DeployedContracts
class DeployedContractsSerializer(serializers.ModelSerializer):
class Meta:
model = DeployedContracts
fields = ('house_no', 'rent', 'security_deposit', 'house_owner', 'house_tenant', 'deployed_contract_address')
i want to write an API view which a contains a get method which takes an email as parameter and returns all the objects of DeployedContract where either house_owner or house_tenant's email is equal to the email provided in parameter. How can i do that in django?
Upvotes: 1
Views: 1423
Reputation: 3827
Implement the filtration part in the get_queryset
function. And OR operation can be implemented using django models Q object.
Now if you call the api with out any parameters, it will return all the DeployedContracts. If you pass any emails, This will return all the filtered result
from django.db.models import Q
from rest_framework.response import Response
from rest_framework.views import APIView
from deployedcontracts.models import DeployedContracts
class DeployedContractsList(APIView):
def get_queryset(self):
deployed_contracts = DeployedContracts.objects.all()
email = self.request.query_params.get('email')
if email:
deployed_contracts = deployed_contracts.filter(
Q(house_owner__iexact=YOURPARAMETER) |
Q(house_tentant__iexact=YOURPARAMETER)
)
return deployed_contracts
def get(self, request, format=None):
service_providers = self.get_queryset()
serializer = DeployedContractsSerializer(service_providers, many=True)
return Response(data=serializer.data)
Upvotes: 1
Reputation: 88539
from rest_framework.response import Response
from rest_framework.views import APIView
from django.db.models import Q
class DeployedContractsAPI(APIView):
def get(self, request, *args, **kwargs):
email = request.data.get('email', None)
if email:
queryset = DeployedContracts.objects.filter(Q(house_owner=email) | Q(house_tentant=email))
serializer = DeployedContractsSerializer(queryset)
return Response(data=serializer.data)
return Response(data={"message": "email parameter not provided"})
You can read more about Django Q expressions here
Upvotes: 1
Reputation: 56
You can import a Q object and try this:
from django.db.models import Q
qs = DeployedContracts.objects.filter(
Q(house_owner__iexact=YOURPARAMETER) |
Q(house_tentant__iexact=YOURPARAMETER)
)
Read the docs for more.
Upvotes: 1