姜戈 |即使我将其设置为 NULL=True,也需要小数字段
Django | Decimal Field is required even tho i set it to NULL=True
我正在尝试使用 django,但我遇到了 运行 问题...
我有一个不需要的小数字段,所以我将它设置为“blank=True”和“null=True”。但它仍然说它是必需的:(
我也做了所有的迁移。
这是我的models.py
from django.db import models
weightUnit = {
('kg' , 'kilogram'),
('g', 'gram'),
('t', 'tons'),
('n', '-'),
}
class Product(models.Model):
pname = models.CharField(
max_length=50,
)
pdesc = models.TextField(
max_length=5000,
)
pprice = models.DecimalField(
max_digits=6,
decimal_places=2,
)
psn = models.CharField(
max_length = 30,
null=True,
blank=True,
)
pweightunit = models.CharField(
choices=weightUnit,
default='n',
null=True,
blank=True,
max_length=5,
)
pweight = models.DecimalField(
null=True,
blank = True,
max_digits=10000,
decimal_places=2,
)
plimage = models.ImageField(
blank=True,
null=True,
)
这是我的 forms.py
from django import forms
from .models import weightUnit
class RawProductForm(forms.Form):
name = forms.CharField(label="Name")
desc = forms.CharField(label="Beschreibung")
price = forms.DecimalField(label="Stückpreis")
sn = forms.CharField(label="Seriennummer")
weightunit = forms.ChoiceField(choices=weightUnit, label="Gewichteinheit")
weight = forms.DecimalField(label="Gewicht")
image = forms.ImageField(label="Bild")
这是我的 views.py
def product_add(request):
pf = RawProductForm()
if request.method == "POST":
pf = RawProductForm(request.POST)
if pf.is_valid():
print(pf.cleaned_data)
Product.objects.create(**pf.cleaned_data)
else:
print(pf.errors)
context = {
"productform" : pf,
}
return render(request, "product_add.html", context)
您正在使用简单的 Form
,而不是 ModelForm
[Django-doc],这意味着它根本不会检查模型。它只会呈现一个表单。 ModelForm
将检查模型并根据您可以自定义的模型构建表单。
class RawProductForm(<b>forms.ModelForm</b>):
class Meta:
<b>model = Product</b>
labels = {
'name': 'Name',
'desc': 'Beschreibung',
'price': 'Stückpreis',
'sn': 'Seriennummer',
'weightunit': 'Gewichteinheit',
'weight': 'Gewicht',
'image': 'Bild',
}
一个ModelForm
还有一个.save(…)
method [Django-doc],它根据表单中的数据创建一个模型对象,并将其保存到数据库中。
我正在尝试使用 django,但我遇到了 运行 问题... 我有一个不需要的小数字段,所以我将它设置为“blank=True”和“null=True”。但它仍然说它是必需的:(
我也做了所有的迁移。
这是我的models.py
from django.db import models
weightUnit = {
('kg' , 'kilogram'),
('g', 'gram'),
('t', 'tons'),
('n', '-'),
}
class Product(models.Model):
pname = models.CharField(
max_length=50,
)
pdesc = models.TextField(
max_length=5000,
)
pprice = models.DecimalField(
max_digits=6,
decimal_places=2,
)
psn = models.CharField(
max_length = 30,
null=True,
blank=True,
)
pweightunit = models.CharField(
choices=weightUnit,
default='n',
null=True,
blank=True,
max_length=5,
)
pweight = models.DecimalField(
null=True,
blank = True,
max_digits=10000,
decimal_places=2,
)
plimage = models.ImageField(
blank=True,
null=True,
)
这是我的 forms.py
from django import forms
from .models import weightUnit
class RawProductForm(forms.Form):
name = forms.CharField(label="Name")
desc = forms.CharField(label="Beschreibung")
price = forms.DecimalField(label="Stückpreis")
sn = forms.CharField(label="Seriennummer")
weightunit = forms.ChoiceField(choices=weightUnit, label="Gewichteinheit")
weight = forms.DecimalField(label="Gewicht")
image = forms.ImageField(label="Bild")
这是我的 views.py
def product_add(request):
pf = RawProductForm()
if request.method == "POST":
pf = RawProductForm(request.POST)
if pf.is_valid():
print(pf.cleaned_data)
Product.objects.create(**pf.cleaned_data)
else:
print(pf.errors)
context = {
"productform" : pf,
}
return render(request, "product_add.html", context)
您正在使用简单的 Form
,而不是 ModelForm
[Django-doc],这意味着它根本不会检查模型。它只会呈现一个表单。 ModelForm
将检查模型并根据您可以自定义的模型构建表单。
class RawProductForm(<b>forms.ModelForm</b>):
class Meta:
<b>model = Product</b>
labels = {
'name': 'Name',
'desc': 'Beschreibung',
'price': 'Stückpreis',
'sn': 'Seriennummer',
'weightunit': 'Gewichteinheit',
'weight': 'Gewicht',
'image': 'Bild',
}
一个ModelForm
还有一个.save(…)
method [Django-doc],它根据表单中的数据创建一个模型对象,并将其保存到数据库中。