【发布时间】: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