Django:在字符串格式化过程中并非所有参数都被转换

时间:2017-06-12 14:48:09

标签: django django-forms foreign-keys

我在Django项目中的模型和表单之间遇到了一些问题。 我创建了一个新应用:Divorce

我的 models.py 文件如下所示:

#-*- coding: utf-8 -*-

from django.db import models
from Identity.models import Person
from Mariage.models import Acte_Mariage
from django.utils.encoding import force_text
from django_countries.fields import CountryField


class Acte_Divorce(models.Model):

    fk_epoux = models.ForeignKey(Person, related_name='EpouxDivorce', verbose_name='Epoux', null=False)
    fk_epouse = models.ForeignKey(Person, related_name='EpouseDivorce', verbose_name='Epouse', null=False)

    fk_temoin1 = models.ForeignKey(Person, related_name='Temoin 1 Divorce+', verbose_name='Temoin 1', null=True)
    fk_temoin2 = models.ForeignKey(Person, related_name='Temoin 2 Divorce+', verbose_name='Temoin 2', null=True)
    fk_temoin3 = models.ForeignKey(Person, related_name='Temoin 3 Divorce+', verbose_name='Temoin 3', null=True)
    fk_temoin4 = models.ForeignKey(Person, related_name='Temoin 4 Divorce+', verbose_name='Temoin 4', null=True)

    mairie = models.CharField(max_length=30, null=False, verbose_name='Mairie', default='  ')

    fk_mariage = models.ForeignKey(Acte_Mariage, related_name='ID', verbose_name="ID", null=False, default= '0')

    divorce_date = models.DateField(null=True, blank=True, verbose_name='Date du mariage (optionnel)')
    divorce_heure = models.TimeField(null=True, blank=True, verbose_name='Heure du mariage (optionnel)')

    created = models.DateTimeField(auto_now_add=True)
    utilisateur = models.CharField(max_length=100, null=False, verbose_name="utilisateur", default=" ")

如您所见,我有两个字段,其中包含两个不同的Foreign_Key:第一个指向Person model,第二个指向Acte_Mariage model

现在,我的Divorce应用程序中有一个 forms.py 文件:

class CustomLabelModelChoiceField(forms.ModelChoiceField):

    def __init__(self, *args, **kwargs):
        self._label_from_instance = kwargs.pop('label_func', force_text)
        super(CustomLabelModelChoiceField, self).__init__(*args, **kwargs)

    def label_from_instance(self, obj):
        return self._label_from_instance(obj)

class Acte_Divorce_Form(forms.ModelForm):
    fk_epoux = CustomLabelModelChoiceField(Person.objects.filter(sex = "Masculin"), required=False, label = "Epoux", label_func=lambda obj: '%s %s %s %s %s' % (obj.lastname, obj.firstname, obj.birthday, obj.birthcity, obj.social_number),  empty_label=None)
    fk_epouse = CustomLabelModelChoiceField(Person.objects.filter(sex = "Feminin"), required=False, label = "Epouse", label_func=lambda obj: '%s %s %s %s %s' % (obj.lastname, obj.firstname, obj.birthday, obj.birthcity, obj.social_number), empty_label=None)

    fk_mariage = CustomLabelModelChoiceField(Acte_Mariage.objects.filter(), label="N° Acte Mariage", label_func=lambda obj: '%s %s %s' % (obj.id, obj.fk_epoux, obj.fk_epouse))

    fk_temoin1 = CustomLabelModelChoiceField(Person.objects.filter(Q(sex='Masculin') | Q(sex='Feminin')), required=False, label = "Témoin 1", label_func=lambda obj: '%s %s %s %s %s' % (obj.lastname, obj.firstname, obj.birthday, obj.birthcity, obj.social_number), empty_label=None)
    fk_temoin2 = CustomLabelModelChoiceField(Person.objects.filter(Q(sex='Masculin') | Q(sex='Feminin')), required=False, label = "Témoin 2", label_func=lambda obj: '%s %s %s %s %s' % (obj.lastname, obj.firstname, obj.birthday, obj.birthcity, obj.social_number), empty_label=None)
    fk_temoin3 = CustomLabelModelChoiceField(Person.objects.filter(Q(sex='Masculin') | Q(sex='Feminin')), required=False, label = "Témoin 3", label_func=lambda obj: '%s %s %s %s %s' % (obj.lastname, obj.firstname, obj.birthday, obj.birthcity, obj.social_number), empty_label=None)
    fk_temoin4 = CustomLabelModelChoiceField(Person.objects.filter(Q(sex='Masculin') | Q(sex='Feminin')), required=False, label = "Témoin 4", label_func=lambda obj: '%s %s %s %s %s' % (obj.lastname, obj.firstname, obj.birthday, obj.birthcity, obj.social_number), empty_label=None)

    mairie = forms.CharField(widget=forms.HiddenInput(), initial=Mairie.objects.using('default').last().city.encode('utf-8'))
    utilisateur = forms.CharField(widget=forms.HiddenInput())

    class Meta :
        model = Acte_Divorce
        fields = ['fk_epoux', 'fk_epouse', 'fk_mariage', 'fk_temoin1', 'fk_temoin2', 'fk_temoin3', 'fk_temoin4', 'divorce_date', 'divorce_heure', 'mairie', 'utilisateur']
        widgets = {'country': CountrySelectWidget()}

    def __init__(self, *args, **kwargs):    
        super(Acte_Divorce_Form, self).__init__(*args, **kwargs)
        for key, value in self.fields.iteritems() :
            self.fields[key].widget.attrs.update({'class':'form-fields'})

class Acte_Divorce_Form(forms.ModelForm)中,我有fk_mariage标签功能。

如果我写:

fk_mariage = CustomLabelModelChoiceField(Acte_Mariage.objects.filter(), label="N° Acte Mariage", label_func=lambda obj: '%s' % (obj.id))

它有效,但我想再显示两个字段:

fk_mariage = CustomLabelModelChoiceField(Acte_Mariage.objects.filter(), label="N° Acte Mariage", label_func=lambda obj: '%s %s %s' % (obj.id, obj.fk_epoux, obj.fk_epouse))

现在我收到了这个错误:not all arguments converted during string formatting

我需要用哪个表达式替换%sobj.fk_epoux的{​​{1}}?两个字段都给了我一个数字。

谢谢

编辑:

来自Mariage应用程序的模型:

obj.fk_epouse

1 个答案:

答案 0 :(得分:1)

试试这个,

fk_mariage = CustomLabelModelChoiceField(Acte_Mariage.objects.filter(), label="N° Acte Mariage", label_func=lambda obj: '{} {} {}'.format(str(obj.id), str(obj.fk_epoux), str(obj.fk_epouse)))

这是