Friday, October 30, 2015

Re: Translation is not working on ModelForm when widget is specified for form field

okkk now i dont hv time whenever i get ill let u know


On 30 October 2015 at 17:20, Sean Xu <seanxu1984@gmail.com> wrote:
Hi Andréas,

I have now applied your second approach and now the field names get translated into Chinese as expected...
Thanks so much.

On Friday, October 30, 2015 at 6:12:21 PM UTC+8, Andréas Kühne wrote:
Hi,

The problem is that you are redifining the fields, the fields then don't get any labels (because you haven't specified them). You can achieve what you want in two ways:

Either add the following to your modelform:
start_time = forms.DateTimeField(label=_("start time"), widget=SplitDateTimeWidget)
end_time = forms.DateTimeField(label=_("end time"), widget=SplitDateTimeWidget)

But I would much rather remove the start_time / end_time definitions in your model form and instead add this under the "class Meta:" heading:
widgets = {
    'start_time': SplitDateTimeWidget,
    'end_time': SplitDateTimeWidget,
}

That way you don't have to add the labels once more, but just use them from the model instead.

Regards,

Andréas

2015-10-30 7:21 GMT+01:00 Sean Xu <seanx...@gmail.com>:
Hi,

I was learning Django Translation using django-swingtime as project with Django 1.7.9 installed.
And I found out when Non-default widgets was originally specified for form fields, the preferred language did not show up from my Firefox browser.
The problem has gone when these widgets were removed.
I'm not sure if this is a bug of Django or was there anything I did wrong.

Note: po and mo file can be created correctly with or without the widgets in place.

You can find the Model definition from below:
class Occurrence(models.Model):
   
'''
    Represents the start end time for a specific occurrence of a master ``Event``
    object.
    '''

    start_time
= models.DateTimeField(_('start time'))
    end_time
= models.DateTimeField(_('end time'))
   
event = models.ForeignKey(Event, verbose_name=_('event'), editable=False)
    notes
= GenericRelation(Note, verbose_name=_('notes'))


    objects
= OccurrenceManager()


   
#===========================================================================
   
class Meta:
        verbose_name
= _('occurrence')
        verbose_name_plural
= _('occurrences')
        ordering
= ('start_time', 'end_time')


And the ModelForm is defined as follows:
class SingleOccurrenceForm(forms.ModelForm):
   
'''
    A simple form for adding and updating single Occurrence attributes


    '''



    start_time
= forms.DateTimeField(widget=SplitDateTimeWidget)
    end_time
= forms.DateTimeField(widget=SplitDateTimeWidget)
   
# Translation works if these 2 lines get removed


   
#===========================================================================
   
class Meta:
        model
= Occurrence
       
if FIELDS_REQUIRED:
            fields
= "__all__"


Br
Sean

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/52a50110-4c9d-4f71-9ebf-6ade058fa47e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d350c903-7285-4932-8c9f-0c5c1a4407f4%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADQRGB4zt1rgjjiv1tJ8xoF20A%2BKs5YWuafBzCOOevgb7U2sCQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment