python - Create a MultipleChoiceField using rows from another model, -
i creating online job application company multiple locations. allow applicant able select checkboxes represent every store apply (could multiple). hesitant hard code these scalability purposes, had hoped create 2 models (i have more that, example these 2 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 (i think) add multiplechoicefield in applicant model, creates choices of instances of store (one each row). far, have tried in applicant class:
def get_stores(): self.stores = store.objects.all()
but unable (as far can tell) grab instances of store had hoped. here few questions have:
- is possible reference model that?
- is referencing store model applicant model right beginning creating several checkboxes let applicant select of stores applying (and allow list change dynamically)?
- is multipleselectfield best way once have pulled of store instances?
this seems canonical use case 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 display form in field, should automatically use multipleselectfield
ref: https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/
Comments
Post a Comment