2

I've got a model which represents statuses for various languages so I'm using the django-hvad TranslatableModel base class.

When used in a form the event_status field choices from the TranslatableModel are 1, 2 etc so Status.__unicode__ must be returning the pk opposed to the description attribute as intended.

forms.py

class StatusForm(forms.ModelForm):
    """
    Form for approving an event.
    """
    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        self.base_fields['user'].queryset = User.objects.filter(pk=user.id)
        super(StatusForm, self).__init__(*args, **kwargs)

    user = forms.CharField(
        widget=forms.HiddenInput()
    )
    timestamp = forms.DateField(
        input_formats=DATE_INPUT_FORMATS
    )

    class Meta:
        model = StatusHistory
        fields = [
            'event_status', 'timestamp', 'user'
        ]

models.py

class Status(TranslatableModel):
    """
    Status to be applied to an event.
    """
    translations = TranslatedFields(
        description=models.CharField(
            verbose_name=_("Status description"),
            default=_("Submitted"),
            max_length=255
        )
    )

    class Meta:
        app_label = 'events'
        verbose_name = _("Status")
        verbose_name_plural = _("Status")

    def __unicode__(self):
        return self.safe_translation_getter('description', str(self.pk))


class StatusHistory(models.Model):
    """
    The historical log of event statuses.
    """
    event = models.ForeignKey(
        'events.Event',
        related_name='StatusHistory'
    )
    event_status = models.ForeignKey(Status)
    timestamp = models.DateTimeField(
        verbose_name=_("Time of status change"),
        auto_now=True
    )
    user = models.ForeignKey(User)

    class Meta:
        app_label = 'events'
        verbose_name = _("Status history")
        verbose_name_plural = _("Status history")

    def __unicode__(self):
        return u"{event}: {status}".format(
            event=self.event, status=self.event_status
        )

Have I implemented the TranslatableModel incorrectly? I had one time while running this through Pycharm's debug server where 1 of the Status objects gave me the description, but the other only returned an id again so I assume I'm correct in this approach.

oz123
  • 23,317
  • 25
  • 106
  • 169
markwalker_
  • 9,401
  • 7
  • 51
  • 86

0 Answers0