modelform foreignkey choices

8 views
Skip to first unread message

Recep KIRMIZI

unread,
Aug 2, 2013, 7:22:24 AM8/2/13
to djan...@googlegroups.com
Merhabalar;

model içinde choices tuple ile aldığım bir veri var (CONSULATE_CHOICES). Admin sayfasında bir problem yok. choices olması gerektiği gibi çalışıyor. Fakat bu modelden bir form oluşturduğumda. template 'e gelen değer tuple 'ın [x][0] 'ıncı elemanı.
CONSULATE_CHOICES[0][0], CONSULATE_CHOICES[1][0], CONSULATE_CHOICES[2][0].  ank, ist, izm   gibi.. 
Çünkü model'in __unicode__() tanımlamasında self.name geri dönüyor. self.name ise 3 karakterlik bir charfield. Template'e bu tuple'ı doğru şekilde nasıl gönderebilirim. Aklıma gelen şey: Form'un init'inde bu alanı yakalayıp  (self.fields['consulate']) widget'ın choices kısmına modelde verdiğim choices 'i eklemek. Ama bu hack mi olur doğru yaklaşım mı olur emin olamadım. Tavsiyeniz nedir ? 

Meseleyi daha iyi anlatabilmek için aşağıda koddan bazı kesitler bulunuyor:


Model:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONSULATE_CHOICES = (
    ('ist', 'İstanbul'),
    ('ank', 'Ankara'),
    ('izm', 'İzmir'),
)

class Consulate(models.Model):
    name = models.CharField(_('Consulate Name'), help_text=_('Please select the consulate'), max_length=3, choices=CONSULATE_CHOICES)
    free_slots = models.PositiveSmallIntegerField(_('Free Slot'), help_text=_('Select Free Slots'), choices=((x, x) for x in range(100)))

    def __unicode__(self):
        return u'%s' % (self.name)

    class Meta:
        verbose_name = _('Consulate')
        verbose_name_plural = _('Consulates')



class Customer(models.Model):
    nationality = models.CharField(_('Nationality'), help_text=_('Please select your nationality'), max_length=3, choices=NATIONALITY_CHOICES)
    purpose_of_visit = models.CharField(_('Purpose of Visit'), max_length=3, choices=PURPOSE_OF_VISIT_CHOICES)
    number_of_visitors = models.IntegerField(_('Number of Visitors'), choices=((x, x) for x in range(1,11)))
    ..........
    consulate = models.ForeignKey('Consulate')

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Form: 
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class ScheduleForm(forms.ModelForm):
    class Meta:
        model = Customer
        # exclude = ['consulate']
        widgets = {'city': TRProvinceSelect() }

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


--
Saygılarımla... Recep KIRMIZI
Reply all
Reply to author
Forward
0 new messages