Django:如何创建类别 field/dropdown 菜单?
Django: how can I a create categories field/dropdown menu?
这是一个非常简单的问题,我如何在 Django 中创建仅包含特定类别的下拉字段(类似于国家/地区下拉列表,但不是国家/地区)。
Django 最强大的功能是为您提供直接表单。
这是一个广泛的问题,但您想定义一个模型,该模型可以与可以放入模板的表单配对。
看看这里:https://docs.djangoproject.com/en/1.8/topics/forms/ 和这里:
Django options field with categories and here Creating a dynamic choice field
如果应动态创建类别,则使用 choice attribute of a Field if it is for fixed values. Or with a ForeignKey 字段。
对于 ForeignKey 字段,您将执行以下操作:
from django.db import models
class Category(models.Model):
name = models.Charfield(max_length=255)
# ...
def __str__(self):
return self.name
class Item(models.Model):
category = models.ForeignKey(Category)
# ...
这是一个非常简单的问题,我如何在 Django 中创建仅包含特定类别的下拉字段(类似于国家/地区下拉列表,但不是国家/地区)。
Django 最强大的功能是为您提供直接表单。 这是一个广泛的问题,但您想定义一个模型,该模型可以与可以放入模板的表单配对。 看看这里:https://docs.djangoproject.com/en/1.8/topics/forms/ 和这里: Django options field with categories and here Creating a dynamic choice field
如果应动态创建类别,则使用 choice attribute of a Field if it is for fixed values. Or with a ForeignKey 字段。
对于 ForeignKey 字段,您将执行以下操作:
from django.db import models
class Category(models.Model):
name = models.Charfield(max_length=255)
# ...
def __str__(self):
return self.name
class Item(models.Model):
category = models.ForeignKey(Category)
# ...