# forms.py
from django import forms
from .models import Post # 假设你的模型定义在当前应用的models.py中
class SavePost(forms.ModelForm):
# 定义选项
CHOICES = [
('Process', 'Process'),
('Product', 'Product'),
('Equipment', 'Equipment'),
]
# 使用ChoiceField来处理单选下拉菜单
dep = forms.ChoiceField(choices=CHOICES, label="Department/Category")
user = forms.IntegerField(help_text="User Field is required.")
title = forms.CharField(max_length=250, help_text="Title Field is required.")
description = forms.CharField(widget=forms.Textarea(), label="Description") # 明确指定Textarea widget
class Meta:
model = Post
fields = ('user', 'title', 'description', 'file_path', 'file_path2', 'dep')
# forms.py
from django import forms
from .models import Post # 假设你的模型定义在当前应用的models.py中
class SavePost(forms.ModelForm):
CHOICES = [
('Process', 'Process'),
('Product', 'Product'),
('Equipment', 'Equipment'),
]
# 使用MultipleChoiceField来处理多选下拉菜单
dep = forms.MultipleChoiceField(choices=CHOICES, label="Department/Category",
widget=forms.CheckboxSelectMultiple) # 可以使用CheckboxSelectMultiple或SelectMultiple
user = forms.IntegerField(help_text="User Field is required.")
title = forms.CharField(max_length=250, help_text="Title Field is required.")
description = forms.CharField(widget=forms.Textarea(), label="Description")
class Meta:
model = Post
fields = ('user', 'title', 'description', 'file_path', 'file_path2', 'dep')
# forms.py
from django import forms
from .models import Post, Department
class SavePost(forms.ModelForm):
# 使用ModelMultipleChoiceField处理多对多关系
dep = forms.ModelMultipleChoiceField(
queryset=Department.objects.all(),
label="Department/Category",
widget=forms.SelectMultiple # 默认就是SelectMultiple
# 或者 widget=forms.CheckboxSelectMultiple for checkboxes
)
# ... 其他字段
class Meta:
model = Post
fields = ('user', 'title', 'description', 'file_path', 'file_path2', 'dep')