1

I am creating an online job application for a company with multiple locations. I would like to allow the applicant to be able to select checkboxes that represent every store they would like to apply to (could be multiple). I am hesitant to hard code these for scalability purposes, so I had hoped to create 2 models (I have more than that, but for this example these are the only 2 that are relevant):

Applicant

class Applicant(models.Model):
    name = models.CharField(max_length=200)
    city = models.CharField(max_length=200)
    state = models.CharField(max_length=200)
    zip = models.CharField(max_length=200)
    social_security_number = models.CharField(max_length=200)
    phone = models.CharField(max_length=200)
    alt_phone = models.CharField(max_length=200, blank=True)
    us_citizen = models.BooleanField()
    committed_felony = models.BooleanField()
    is_16 = models.BooleanField()
    has_drivers_license = models.BooleanField()
    is_disabled = models.BooleanField()
    prev_employed = models.BooleanField()
    felony_explanation = models.TextField(blank=True)
    disabled_explanation = models.TextField(blank=True)
    prev_employment_manager = models.CharField(max_length=200, blank=True)
    prev_employment_year = models.CharField(max_length=4, blank=True)
    skills = models.TextField()
    was_completed = models.BooleanField(default=False)

    def __unicode__(self):
        return self.name

Store

class Store(models.Model):
    code = models.CharField(max_length=10)
    description = models.CharField(max_length=200)
    city = models.CharField(max_length=20)
    state = models.CharField(max_length=20)

    def __unicode__(self):
        return self.description

I would (I think) like to add a MultipleChoiceField in the applicant model, that creates choices from all of the instances of Store (one for each row). So far, I have tried this in the applicant class:

def get_stores():
        self.stores = Store.objects.all()

but was unable to (as far as I can tell) grab the instances of Store like I had hoped. Here are a few questions I have:

  • Is it even possible to reference another model like that?
  • Is referencing the Store model from the Applicant model the right beginning for creating several checkboxes to let an applicant select all of the stores they are applying to (and allow the list to change dynamically)?
  • is a MultipleSelectField the best way to do this once I have pulled all of the Store instances?

1 Answer 1

1

This seems like the canonical use case for ManyToManyField.

class Store(models.Model):
    ...

class Applicant(models.Model):
    name = models.CharField(max_length=200)
    ...
    was_completed = models.BooleanField(default=False)

    stores = ManyToManyField(Store, related_name='applicants')

When you display this form in a field, it should automatically use a MultipleSelectField

REF: https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/

Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm - the documentation is black and white, I just never found this section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.