django - Display full names in Form ChoiceField but saving ID's -


i have model person - database copied person_id custom_id.

models.py

class employee(models.model):     custom_id = models.charfield(max_length=20, unique=true)      @property     def person(self):         return person.objects.get(person_id='%s' % self.custom_id)      def __str__(self):         return '%s' % self.custom_id  class task(models.model):     employee = models.manytomanyfield(employee, blank=true, null=true)     task = models.charfield(max_length=100)     comment = models.charfield(max_length=200)      def __str__(self):         return '%s' % self.task 

i add method person() employee allow me access other objects model in database:

so when type in shell:

employee.objects.get(custom_id='123').person.full_name  u'adam dylan' 

i have modelform use modelmultiplechoicefield

forms.py

class taskcreateform(forms.modelform):      employee = forms.modelmultiplechoicefield(queryset=employee.objects.all())      class meta:         model = task 

but employee.objects.all() returns bunch of custom_id's. want show in form "employee(..).person.full_name" saving custom_id's.

i not sure why think answer gave other question not work here. did try following? if not work, how fail?

class employeemultiplechoicefield(modelmultiplechoicefield):     def label_from_instance(self, obj):         return obj.person.full_name  class taskcreateform(forms.modelform):     employee = employeemultiplechoicefield(queryset=employee.objects.all())      class meta:         model = task 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -