Django: values_list() multiple fields concatenated

后端 未结 3 897
予麋鹿
予麋鹿 2021-02-19 02:16

I have a Person model and I am using a django form to edit another object with a foreign key to Person. The person model has first_name an

3条回答
  •  误落风尘
    2021-02-19 03:00

    Per: Is it possible to reference a property using Django's QuerySet.values_list?, avoiding values_list when not applicable, using a comprehension instead.

    models.py:
    class Person(models.Model):
        first_name = models.CharField(max_length=32)
        last_name = models.CharField(max_length=64)
        def getPrintName(self):
            return self.last_name + ", " + self.first_name
    
    views.py:
    data.form.fields['person'].choices = [(person.id, person.getPrintName()) for person in GetPersons()]
    

提交回复
热议问题