2

我有一个代表各种语言状态的模型,所以我使用 django-hvadTranslatableModel基类。

当在表单中使用时,来自are等的event_status字段选择必须按预期返回与描述属性相反的值。TranslatableModel1, 2Status.__unicode__pk

表格.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'
        ]

模型.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
        )

我是否执行TranslatableModel不正确?我有一次通过 Pycharm 的调试服务器运行它,其中 1 个Status对象给了我描述,但另一个只返回了一个 id,所以我认为我在这种方法中是正确的。

4

0 回答 0