Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

python - Displaying Foreign Key Options in django restframework view

I'm having trouble displaying all the jobs as options in the apply url view see image below.

enter image description here

I am getting the error which says

Lists are not currently supported in HTML input

The main function I am looking for is for a list of jobs that were posted to be available for selection when applying for the job.

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)

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


class Applicant(models.Model):
  """A applicant that can apply to a job"""
  job = models.ForeignKey(Job, related_name='applicants', 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

I've removed some of the attributes in Job so that the code is not so long.

serializers.py

from rest_framework import serializers
from django.utils.translation import ugettext_lazy as _

from core.models import Job, Applicant


class JobSerializer(serializers.ModelSerializer):
  """Serializer for tag objects"""
  applicants = serializers.StringRelatedField(many=True)

  class Meta:
    model = Job
    fields = ('id', 'description', 'job_type', 'city', 'state', 'salary', 'position', 'employer', 'created_date', 'is_active', 'applicants')
    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"""
  jobs = JobSerializer(many=True, queryset=Job.objects.all())

  class Meta:
    model = Applicant
    fields = ('id','jobs', '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

I've tried adding a queryset=Job.objects.all() as an argument to the JobSerializer() in the ApplyJobSerializer class in my serializers.py field. However I get an error that says

TypeError: __init__() got an unexpected keyword argument 'queryset'

question from:https://stackoverflow.com/questions/65909817/displaying-foreign-key-options-in-django-restframework-view

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can select an existing job in the form.

class JobSerializer(serializers.PrimaryKeyRelatedField, serializers.ModelSerializer):
    """Serializer for tag objects"""
    applicants = serializers.StringRelatedField(many=True)

    class Meta:
        model = Job
        fields = ('__all__')
        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"""
    jobs = JobSerializer(many=True, queryset=Job.objects.all())

    class Meta:
        model = Applicant
        fields = ('__all__')
        read_only_fields = ('id',)

This result as I have created a new job with description is desc enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...