我已经被困在这个问题上好几个小时了,查看Django文档和调整我的代码都没有帮助。
我手动编写了一个HTML表单(出于样式方面的原因)。在我尝试通过这个表单编辑一个对象之前,一切都很好。
我使用的是没有添加的标准UpdateView,但由于某种原因,表单没有填充对象中的数据。
class RPLogbookEditEntry(LoginRequiredMixin, UpdateView):
login_url = 'ihq-login'
template_name = 'internethq/ihq-rplogbookform.html'
model = RPLogbookEntry
fields = ['entry_date', 'rpa_code', 'rpa_icao_code','rpa_registration', 'user_role', 'crew', 'crew_role', 'launch_location', 'recovery_location',
'remarks', 'vlos', 'evlos1', 'evlos2', 'bvlos', 'ft_day', 'ft_night', 'remarks', 'no_of_landings_day', 'no_of_landings_night']
def form_valid(self, form):
form.instance.user = self.request.user
form.instance.rpa = RPA.objects.get(rpa_code=form.cleaned_data['rpa_code'])
return super().form_valid(form)
def form_invalid(self, form):
return super().form_invalid(form)
def get_success_url(self):
return reverse('ihq-rplogbook-list') + '?month=' + str(self.object.entry_date.month) + '&year=' + str(self.object.entry_date.year)models.py
class RPLogbookEntry(models.Model):
## Date
entry_date = models.DateField()
## RPA Details
rpa_obj = RPA.objects.only('rpa_code')
RPA_CODE_CHOICES = []
for vars in rpa_obj:
RPA_CODE_CHOICES.append((vars.rpa_code, vars.rpa_code))
rpa_code = models.CharField(choices=RPA_CODE_CHOICES, max_length=20)
rpa_icao_code = models.CharField(max_length=15)
rpa_registration = models.CharField(max_length=30)
rpa = models.ForeignKey(RPA, on_delete=models.CASCADE)
## Crew Details
user = models.ForeignKey(User, on_delete=models.CASCADE)
USER_ROLE_CHOICES = [
('RPIC', 'Remote Pilot-In-Command'),
('RP', 'Remote Pilot'),
('SPOT', 'Spotter'),
('CAMOP', 'Camera operator'),
]
user_role = models.CharField(
max_length=5,
choices=USER_ROLE_CHOICES,
)
crew = models.CharField(max_length=128, blank=True)
crew_role = models.CharField(
max_length=5,
choices=USER_ROLE_CHOICES,
blank = True
)
## Flight Details
launch_location = models.CharField(max_length=128)
recovery_location = models.CharField(max_length=128)
remarks = models.CharField(max_length=256, blank=True)
vlos = models.BooleanField(default=True)
evlos1 = models.BooleanField(default=False)
evlos2 = models.BooleanField(default=False)
bvlos = models.BooleanField(default=False)
## Flight Time
ft_day = FlightTimeFieldInt("Day flight time", blank=True, null=True,)
ft_night = FlightTimeFieldInt("Night flight time", blank=True, null=True,)
def _ft_total(self):
return int(self.ft_day + self.ft_night)
ft_total = property(_ft_total)
## Category of mission or flight
MISSION_CATEGORY_CHOICES = [
('INS', 'Inspection'),
('MAP', 'Mapping'),
('PHO', 'Photography'),
('VID', 'Videography'),
('PRI', 'Private flying'),
('TRA', 'Training'),
]
mission_category = models.CharField(
max_length=3,
choices=MISSION_CATEGORY_CHOICES,
blank=True,
)
## Landings & Autolands
no_of_landings_day = models.IntegerField("Number of landings", blank=True, default=0,)
no_of_landings_night = models.IntegerField("Number of auto landings", blank=True, default=0,)
def get_absolute_url(self):
return reverse('ihq-rplogbook-list')
def clean(self):
if self.no_of_landings_day is None and self.no_of_landings_night is None:
raise ValidationError({'no_of_landings_day':_('You must have landed at least once!')})
if self.no_of_landings_night is None:
if self.no_of_landings_day >= 1:
self.no_of_landings_night = 0
return self.no_of_landings_night
if self.no_of_landings_day is None:
if self.no_of_landings_night >= 1:
self.no_of_landings_day = 0
return self.no_of_landings_day
def clean_fields(self, exclude=None):
validation_error_dict = {}
p = re.compile('^[0-9]+:([0-5][0-9])')
if self.ft_day !='' and p.match(self.ft_day) is None:
validation_error_dict['ft_day'] = ValidationError(_('Flight duration must be in HH:MM format.'))
if self.ft_night !='' and p.match(self.ft_night) is None:
validation_error_dict['ft_night'] = ValidationError(_('regex night wrong'))
if self.ft_day =='' and self.ft_night =='':
validation_error_dict['ft_day'] = ValidationError(_('Fill in at least one flying time duration.'))
if (self.vlos or self.evlos1 or self.evlos2 or self.bvlos) is False:
validation_error_dict['vlos'] = ValidationError(_('Select one LOS category.'))
if (self.vlos or self.evlos1 or self.evlos2 or self.bvlos) is True:
counter = 0
for x in [self.vlos, self.evlos1, self.evlos2, self.bvlos]:
if x == True:
counter += 1
if counter > 1:
validation_error_dict['vlos'] = ValidationError(_('Selecting more than one LOS category is not allowed.'))
# if self.no_of_landings is None and self.no_of_auto_landings is None:
# validation_error_dict['no_of_landings'] = ValidationError(_('You must have landed at least once!'))
if validation_error_dict:
raise ValidationError(validation_error_dict)基本上,我的问题是-这个UpdateView不应该自动填充我的表单吗?
发布于 2021-11-01 01:10:12
我犯了个很愚蠢的错误。
我使用相同的HTML模板来添加和编辑这些记录,其中{{form.cleaned_data.field_name}}在<input value="..>标记中。这可以在添加新记录(例如无效表单)时保留数据,但在编辑对象时不会显示。
修复方法是创建一个新的模板(相同的页面),但是在<input value="...中填充必要的对象值模板标记,即{{object.field_name}}。
https://stackoverflow.com/questions/69787382
复制相似问题