2017-06-12 66 views
0

我在我的Django項目中遇到了模型和表單之間的一個小問題。 我創建了一個新的應用程序:DivorceDjango:不是在字符串格式化過程中轉換的所有參數

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。現在

,我在離婚申請有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_epouxobj.fk_epouse?這兩個領域給了我一個數字。

謝謝

編輯:

從婚姻法的應用我的模型:

class Acte_Mariage(models.Model): 

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

    fk_father_husband = models.ForeignKey(Person, related_name='Pere Epoux+', verbose_name='Pere Epoux', null=False) 
    fk_mother_husband = models.ForeignKey(Person, related_name='Mere Epoux+', verbose_name='Mere Epoux', null=False) 
    fk_father_wife = models.ForeignKey(Person, related_name='Pere Epouse+', verbose_name='Pere Epouse', null=False) 
    fk_mother_wife = models.ForeignKey(Person, related_name='Mere Epouse+', verbose_name='Mere Epouse', null=False) 

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

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

    mariage_date = models.DateField(null=True, blank=True, verbose_name='Date du mariage (optionnel)') 
    mariage_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=" ") 
+2

你的模特不允許同性婚姻? – MichielB

+0

請分享你的模型'Acte_Mariage' –

+0

@MichielB不是暫時的,因爲這個項目將安裝的國家不允許同性婚姻。這可能是下一次更新;) – Deadpool

回答

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))) 

+0

噢,是的,我使用了你的解決方案,它完美的工作!但爲什麼我必須用'{}'而不是'%s'來指定每個對象的格式? – Deadpool

+0

'%s's在Python 2.x中,python3支持'{}'爲字符串格式化 – zaidfazil

+0

很奇怪,因爲我使用的是Python 2.7 oO – Deadpool

相關問題