您好,我有2个模型Sight和SightPic,问题是我要进行验证,仅按照某些规则提交表单。我只想将一张图片设为is_front == True,并验证是否存在多于一张或无一张引发错误的形式
在我干净的功能中
print(len(images.filter(is_front=True)))
它会向我显示不是表格中的数据,而是数据库中的数据(这是正常的),我该如何检查表格中的数据?
models.py
class Sight(models.Model):
name=Charfield(...)
description=Charfield(...)
...
class SightPicture(models.Model):
sight=ForeignKey(Sight)
picture=ImageField(...)
is_front=BooleanField
Admin.py
class SightPicInline(admin.TabularInline):
model = SightPicture
fields = ("image_tag", "picture", "is_front")
readonly_fields = ("image_tag",)
class SightAdmin(admin.ModelAdmin):
model = Sight
inlines = [SightPicInline, ...., ....]
admin.site.register(Sight, SightAdmin)
答案 0 :(得分:0)
您可以覆盖ModelAdmin
的clean方法:
from django import forms
class SightAdmin(admin.ModelAdmin):
class Meta:
model = Sight
inlines = [SightPicInline, ...., ....]
def clean(self):
if Sight.objects.filter(is_front=self.cleaned_data.get("is_front")).exists():
raise forms.ValidationError("You can at max select one image as front")
#more validations here
return self.cleaned_data
答案 1 :(得分:0)
由于oracledb.getConnection(
{
user: db.user,
password: db.password,
connectString: db.connectString
},
connExecute
);
function connExecute(err, connection) {
if (err) {
console.error(err.message);
return;
}
sql = `SELECT * FROM db.test`;
connection.execute(sql, {}, { outFormat: oracledb.OBJECT },
function (err, db) {
if (err) {
console.error(err.message);
connRelease(connection);
return;
}
// data update loop
connRelease(connection);
});
}
function connRelease(connection) {
connection.close(
function (err) {
if (err) {
console.error(err.message);
}
});
}
中没有内联模型数据,因此您可以尝试从self.cleaned_data
属性访问内联模型数据,为self.data
模型管理员添加表单,并在其中指定表单类模型管理员Sight
form=SightForm