我的情况非常复杂。
from types import *
from django import forms
from django.db.models import Q
from django.core.exceptions import ValidationError
from schdeules.models import tbmsttemplate,tbmstreviewsched,tbtrnrevdepartments,tbtrnrevdesignations,tbmstappraisalsched,tbtrnappraisalreview,tbmstdepartment,tbmstpart,tbmstsection,tbtrnappraisalquestion
class tbmstappraisalschedForm(forms.ModelForm):
def clean(self):
"""
Override the default clean method to check whether this course has been already inputted.
"""
cleaned_data = super(tbmstappraisalschedForm, self).clean()
depart_id = self.cleaned_data.get('intDeptID')
fromdate = str(self.cleaned_data.get('sdtFromDate'))
todate = str(self.cleaned_data.get('sdtToDate'))
pk=self.instance.pk
res = tbmstappraisalsched.objects.filter(
Q(sdtFromDate__lte=fromdate,sdtToDate__gte=fromdate) | \
Q(sdtFromDate__lte=todate,sdtToDate__gte=todate), ~Q(intAppSchedID=pk),
intDeptID=depart_id.pk,
)
if(res).exists():
raise ValidationError('The slot is present for selected dates and selected department')
else:
res_revs = tbmstreviewsched.objects.filter(
Q(sdtFromDate__lte=fromdate,sdtToDate__gte=fromdate) | \
Q(sdtFromDate__lte=todate,sdtToDate__gte=todate),
tbtrnrevdepartments__intDeptID=depart_id.pk,
)
if(res_revs).exists():
show review scheds such that user can select needed review scheds and save data in two models at once i.e; tbmstappraisalsched,tbtrnappraisalreview
return self.cleaned_data
else:
raise ValidationError('Reviewschedule slots are not present for the selected dates and selected department')
class Meta:
model = tbmstappraisalsched
在上面的代码中,我必须从tbmstreviewsched获取审查时间并向用户显示,以便他可以选择所需的审查时间并一次性将数据保存在两个模型中,这样数据必须保存在tbmstappraisalsched中,返回intappschedid (自动增量值),然后必须使用新的intappschedid和选定的审查sched id将数据保存在tbtrnappraisalreview中。 我的admin.py代码是
from schdeules.models import tbmsttemplate,tbmstreviewsched,tbtrnrevdepartments,tbtrnrevdesignations,tbmstappraisalsched,tbtrnappraisalreview,tbmstpart,tbmstsection,tbtrnappraisalquestion
from django.core.exceptions import ValidationError
from forms import tbmstappraisalschedForm
class QuestionsInline(admin.StackedInline):
model = tbtrnappraisalquestion
extra = 0
class TemplatesAdmin(admin.ModelAdmin):
readonly_fields = ("intUpdatedBy",)
inlines = [QuestionsInline]
def save_model(self, request, obj, form, change):
obj.intUpdatedBy = request.user.id
obj.save()
class ReviewDesignationInline(admin.StackedInline):
model = tbtrnrevdesignations
extra = 1
class ReviewDepartmentInline(admin.StackedInline):
model = tbtrnrevdepartments
exclude = ['intUpdatedBy']
extra = 1
class ReviewScheduleAdmin(admin.ModelAdmin):
inlines = [ReviewDepartmentInline,ReviewDesignationInline]
exclude = ['intUpdatedBy']
def save_model(self, request, obj, form, change):
obj.intUpdatedBy = request.user.id
obj.save()
class AppraisalScheduleAdmin(admin.ModelAdmin):
exclude = ['intUpdatedBy']
form = tbmstappraisalschedForm
def save_model(self, request, obj, form, change):
obj.intUpdatedBy = request.user.id
obj.save()
class AppraisalReviewAdmin(admin.ModelAdmin):
exclude = ['intUpdatedBy']
def save_model(self, request, obj, form, change):
obj.intUpdatedBy = request.user.id
obj.save()
admin.site.register(tbmstpart)
admin.site.register(tbmstsection)
admin.site.register(tbmsttemplate,TemplatesAdmin)
admin.site.register(tbmstreviewsched,ReviewScheduleAdmin)
admin.site.register(tbmstappraisalsched,AppraisalScheduleAdmin)
admin.site.register(tbtrnappraisalreview,AppraisalReviewAdmin)