【问题标题】:int() argument must be a string, a bytes-like object or a number, not 'ForeignKey'int() 参数必须是字符串、类似字节的对象或数字,而不是 'ForeignKey'
【发布时间】:2019-10-09 08:50:55
【问题描述】:

我是 Django 的初学者。

我需要仅显示所选国家/地区存在的 type_of_due_date 的列。

在“class InputType”中,我尝试使用limit_choices_to 和Q 函数,但看到类似的错误

int() 参数必须是字符串、类似字节的对象或数字,而不是 'ForeignKey'#

请帮忙。我做错了什么?

我的模型:

class InputVatDueDate(models.Model):
    country = models.ForeignKey(Country, verbose_name='Country', on_delete=models.CASCADE, db_index=True,
                                blank=True)
    date = models.DateField(_('date'), default=datetime.datetime.now)

    class Meta:
        verbose_name = _('input vat due date')
        verbose_name_plural = _('input vats due date')
        unique_together = (('country', 'date'),)

    def __str__(self):
        return self.country.name


class TypeOfVatDueDate(models.Model):
    vat_due_date = models.ForeignKey(InputVatDueDate, on_delete=models.CASCADE)
    type_of_due_date = models.CharField(max_length=100, choices=enum_to_choice(TypeDueDates))
    date_of_start = models.IntegerField(_('day of start date'))

    def __str__(self):
        return self.type_of_due_date

class InputType(models.Model):
    vat_due_company = models.ForeignKey(CompanyVatDueDate, on_delete=models.CASCADE)
    country = models.ForeignKey(InputVatDueDate, on_delete=models.CASCADE,)
    type_of_due_date = models.ForeignKey(TypeOfVatDueDate, on_delete=models.CASCADE, limit_choices_to=Q(vat_due_date_id=country), )

【问题讨论】:

  • TypeDueDates的代码在哪里?
  • 在您的 IntegerField 之一中,您在保存数据时传递了 ForeignKey,我猜这可能是问题!
  • class TypeDueDates(enum.Enum): Germany_monthly = '德国月刊' Germany_quarterly = '德国季刊' france_monthly = '法国月刊' france_quarterly = '法国季刊' @classmethod def translation(cls): return { cls.germany_monthly: '德国月刊', cls.germany_quarterly: '德国季刊', cls.france_monthly: '法国月刊', cls.france_quarterly: '法国季刊', }
  • 试试limit_choices_to=Q(vat_due_date=F('country'))。但是,我不确定这是否可行,因为F('country') 可能会在相关模型而不是模型本身上执行。否则,我只能按照 Daniel Heppner 在他的回答中所建议的那样以表格形式执行此操作。

标签: django django-models


【解决方案1】:

你的问题在这里:

class InputType(models.Model):
    ...
    type_of_due_date = models.ForeignKey(TypeOfVatDueDate, on_delete=models.CASCADE, limit_choices_to=Q(vat_due_date_id=country), )

我的理解是,您希望将选择限制为 TypeOfVatDueDate 实例,其中 vat_due_datecountry 的值相同

在这个范围内,变量country是模型classForeignKey字段,而不是当前模型instance的字段country的值>.

我不知道在模型级别实现您想要实现的目标。您将不得不在其他地方执行此操作,例如在自定义 ModelForm 中:

class InputTypeForm(forms.ModelForm):

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        # Restrict queryset if country is set
        if self.instance and self.instance.country:
            self.fields['type_of_due_date'].queryset = TypeOfVatDueDate.objects.filter(vat_due_date=country)

    class Meta:
        model = InputType

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多