python - Django Admin - 适用于一个整数模型的选择,但不适用于另一个

标签 python django django-admin

我正在 Django 中制作茶叶目录,但我在模型方面遇到了一些问题。我希望数据库中的某些内容对于某些评级有 1-5 的选择。例如,对于茶类型,我想要 1-5 的咖啡因含量。我使用以下代码执行此操作:

one_to_five_choices = zip(range(1,5+1), range(1,5+1))
...

class TeaType(models.Model):
    name = models.CharField(max_length=20, primary_key=True)
    steeping_temperature = models.PositiveSmallIntegerField()
    steeping_time_minutes = models.PositiveSmallIntegerField()
    caffeine_level = models.PositiveSmallIntegerField(choices=one_to_five_choices)
    directions = models.TextField()

    def __str__(self):
        return self.name

而且效果非常好!

enter image description here

但是,我似乎有相同的评级代码,并且它没有显示此选项框。

class Rating(models.Model):
    rating = models.PositiveSmallIntegerField(choices=one_to_five_choices)
    tea = models.ForeignKey(Tea)
    user = models.ForeignKey(User)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return "{} star rating for {} by {}".format(self.rating, self.tea, self.user.get_username())

    class Meta:
        unique_together = (("user", "tea"),)

这是图片:enter image description here

admin.py 中没有发生任何有趣的事情导致此情况

for m in [TeaType, Brand, Ingredient, Picture, Rating]:
    admin.site.register(m)

知道为什么会发生这种情况吗?

最佳答案

在 python 3 上,zip() 返回一个生成器。您的第一个模型正在耗尽该生成器,因此第二个模型没有选择。将 one_to_ Five_choices 包装在 list() 调用中应该可以解决问题。

关于python - Django Admin - 适用于一个整数模型的选择,但不适用于另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44619763/

相关文章:

python - Django 自定义应用程序配置

django - 您能为管理员推荐一个好的Django文件管理器吗?

python - Django 中全局变量的替代方案?

python - selenium.common.exceptions.NoSuchElementException : Message: Web element reference not seen before using GeckoDriver Firefox and Selenium with Python

python - 将 .png 文件转换为 .nii(NiFti 文件)

javascript - 如何在 django 中像在 Instagram 中一样构建书签系统

python - django 在 View 和模板中使用 url 参数

python - 有人有 PyCharm 的 monokai 主题吗?

python - 如何为现成的数据库制作 django 管理面板?

python - 将对象注入(inject)到类实例中