简体   繁体   中英

Creating view with foreign key model django restframework

I'm fairly new with the django restframework.

I am trying to create a serializer and a view for a model that has a foreignKey .

Models.py

class Job(models.Model):
  """A Job used to create a job posting"""
  user = models.ForeignKey(
    settings.AUTH_USER_MODEL,
    on_delete=models.CASCADE,
  )
  description = models.TextField()
  job_type = models.CharField(max_length=12, choices=JOB_TYPE_CHOICES, default='Full-Time')
  city = models.CharField(max_length=255)
  state = models.CharField(max_length=255)
  created_date = models.DateField(auto_now=False, auto_now_add=True)
  salary = models.CharField(max_length=255)
  position = models.CharField(max_length=255)
  employer = models.CharField(max_length=255)
  is_active = models.BooleanField(default=True)

  def __str__(self):
    return self.description[:50]


class Applicant(models.Model):
  """A applicant that can apply to a job"""
  job = models.ForeignKey(Job, on_delete=models.CASCADE)
  first_name = models.CharField(max_length=255)
  last_name = models.CharField(max_length=255)
  email = models.EmailField(max_length=254)
  phone_number = PhoneNumberField()
  resume = models.FileField(upload_to=resume_file_path, validators=[validate_file_extension])

  def __str__(self):
    return self.first_name

The idea is that Applicant will be able to apply to Job .

I can't seem to figure out how to get the Job id when a Applicant is applying to job.

serializers.py

class JobSerializer(serializers.ModelSerializer):
  """Serializer for tag objects"""

  class Meta:
    model = Job
    fields = ('id', 'description', 'job_type', 'city', 'state', 'salary', 'position', 'employer', 'created_date', 'is_active')
    read_only_fields = ('id',)
 

  def create(self, validated_data):
    """Create a job posting with user and return it"""
    return Job.objects.create(**validated_data)


class ApplyJobSerializer(serializers.ModelSerializer):
  """Serializer for applying to jobs"""

  class Meta:
    model = Applicant
    fields = ('id', 'first_name', 'last_name', 'email', 'phone_number', 'resume')
    read_only_fields = ('id',)

views.py

class ApplyJobView(generics.CreateAPIView):
  """Allows applicants to apply for jobs"""
  serializer_class = serializers.ApplyJobSerializer

Below is an image of my http://localhost:8000/api/jobPosting/apply/ url

I don't know how i can bring in the Jobs as in option to my view so I can obtain the id field.

在此处输入图像描述

I get the following error when POST

null value in column "job_id" violates not-null constraint

You are not passing job field in serializer fields. Try this:

class ApplyJobSerializer(serializers.ModelSerializer):
  """Serializer for applying to jobs"""

  class Meta:
    model = Applicant
    fields = ('id', 'first_name', 'last_name', 'email', 'phone_number', 'resume', 'job')
    read_only_fields = ('id',)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM