python - 从管理页面创建选择

标签 python django django-models

我正在尝试用 django 制作一个学校管理系统。我想在“staff”模型中添加“staff_type”字段。在前端,将从下拉菜单中选择。但我不想对选择进行硬编码。我想添加管理页面中的选项。我怎样才能用 django 做到这一点?

from django.db import models

#this is over-simlified.

class person(models.model):
    name = models.Charfield(max_length =100)
class staff(person):
    staff_type = models.Charfield(max_length =20)

最佳答案

如果您希望能够在管理员中添加员工类型,您可以为此添加另一个模型并拥有该模型的外键。

class StaffType(person):
    name = models.Charfield(max_length=20)

    def __str__(self):
        return self.name


class Staff(person):
    staff_type = models.ForeignKey(StaffType, on_delete=models.PROTECT)

现在,当您在管理员中添加 StaffType 时,当您添加/编辑 Staff 实例时,它将成为可用的选择

关于python - 从管理页面创建选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59452286/

相关文章:

python - 为什么在函数中创建 Tkinter 图像时不显示?

Python、Django 单元测试。 RequestFactory 测试不起作用

python - Django ORM : Equivalent of SQL `NOT IN` ? `exclude`和 `Q`对象不起作用

python - 具有keras和多个序列的时间序列预测

python - gevent和多进程的区别

python - numpy 类型字符串中的 "|"是什么意思?

python - Django 主键序列化器 "This field may not be null"同时allow_null=True

python - 如何在 get_context_data 中执行两个相关的数据库查询,即使用 "request"?

python - 基于此模型触发另一个模型的数据更改(M2M)

Django runserver 给了我语法错误