Okay I have been at this for a while and cannot figure it out. Whenever I make a new job post I get the following error
jobs.models.Positions.DoesNotExist: Positions matching query does not exist.
Business Model:
from django.db import models
from django_google_maps import fields as map_fields
from django.conf import settings
User = settings.AUTH_USER_MODEL
Company = settings.COMPANY_USER_MODEL
class Business(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
business = models.CharField(max_length=50, unique=True)
address = map_fields.AddressField(max_length=200, null=True)
geolocation = map_fields.GeoLocationField(max_length=100, null=True)
about = models.TextField(max_length=1000, null=True)
def __str__(self):
return self.business
Job Model:
from django.db import models
from django_google_maps import fields as map_fields
import uuid as uuid_lib
from django.conf import settings
User = settings.AUTH_USER_MODEL
class Positions(models.Model):
position = models.CharField(max_length=100)
def __str__(self):
return self.position
class Job(models.Model):
employer = models.ForeignKey(User, on_delete=models.CASCADE)
business = models.ForeignKey('business.Business', related_name='jobs', on_delete=models.CASCADE)
position = models.ForeignKey(Positions, on_delete=models.CASCADE)
address = map_fields.AddressField(max_length=200, null=True)
geolocation = map_fields.GeoLocationField(max_length=100, null=True)
uuid = models.UUIDField(db_index=True, default=uuid_lib.uuid4, editable=False)
job_detail = models.TextField() # TODO: set length
full_time = models.BooleanField(default=False)
part_time = models.BooleanField(default=False)
date_posted = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.address
Job Serializer:
from rest_framework import serializers, fields
from .models import Job, Type, Positions
def get_business_model():
return django_apps.get_model(settings.BUSINESS_MODEL, require_ready=False)
Business = get_business_model()
# Position Serializer
class PositionSerializer(serializers.ModelSerializer):
class Meta:
model = Positions
fields = "__all__"
class JobSerializer(serializers.ModelSerializer):
date_posted = serializers.DateTimeField(read_only=True, format='%B %w, %Y')
position = serializers.CharField(source="position.position")
business = serializers.CharField(source="business.business")
def create(self, validated_data):
position = validated_data.pop('position')
position_instance = Positions.objects.get(position=position)
business = validated_data.pop('business')
business_instance = Business.objects.get(business=business)
return Job.objects.create(position=position_instance, business=business_instance, **validated_data)
class Meta:
model = Job
exclude = ['employer', ]
depth = 1
Job Views:
from rest_framework import viewsets, permissions, generics
from rest_framework.mixins import UpdateModelMixin
from .serializer import JobSerializer, PositionSerializer
from .models import Job, User, Positions
class JobListCreateAPIView(generics.ListCreateAPIView):
queryset = Job.objects.all()
permission_classes = [
# permissions.IsAuthenticated
permissions.AllowAny # TODO: change authorization
]
serializer_class = JobSerializer
lookup_field = 'uuid'
def perform_create(self, serializer):
serializer.save(employer=self.request.user)
class JobRetrieveUpdateDestroyAPIView(generics.RetrieveUpdateDestroyAPIView):
queryset = Job.objects.all()
permission_classes = [
# permissions.IsAuthenticated
permissions.AllowAny # TODO: change authorization
]
serializer_class = JobSerializer
lookup_field = 'uuid'
class PositionsAPI(generics.ListAPIView):
permission_classes = [
permissions.AllowAny
]
serializer_class = PositionSerializer
def get_queryset(self):
queryset = Positions.objects.all()
position = self.request.GET.get('position')
if position is not None:
position = queryset.filter(position__istartswith=position)[:5]
return position
I am using this in combination with React and Redux but that doesn't seem to be the problem. I believe it is my serializer. The objects definitely do exist because when I hardcode one of them in it finds it no problem and gets posted
like if I do the following it will work
def create(self, validated_data):
position_instance = Positions.objects.get(position='Bartender')
business_instance = Business.objects.get(business='lameee')
return Job.objects.create(position=position_instance, business=business_instance, **validated_data)
Postman request
My database
Thank You :)
question from:
https://stackoverflow.com/questions/66069035/matching-query-does-not-exist-drf