Django 'NoneType' 对象不可迭代
Django 'NoneType' object is not iterable
我收到以下错误
'/blocks/choices/ 处出现类型错误
NoneType' 对象不可迭代
我做错了什么?
查看
def choices(request):
form = RoomLogChoices()
form.fields['choice'].choices = list()
for i in Room.objects.all():
form.fields['choice'].choices = form.fields['choice'].choices.append(
(
i.room_name,
list(
(o.id, o.room_value) for o in RoomLog.objects.filter(room=i).order_by('room')
)
)
)
形式
class RoomLogChoices(forms.ModelForm):
choice = forms.ModelChoiceField(
queryset=RoomLog.objects.all().order_by('room__room_name', 'room_value')
)
class Meta:
model = RoomLog
fields = '__all__'
编辑:
以下是我要求的模型。出于这些原因,我使用了两个模型:Room 和 RoomLog。 RoomLog 仅用于跟踪 Room 值的更新历史。
型号
class Room(models.Model):
room_name = models.CharField(max_length= 10)
room_value = models.PositiveSmallIntegerField(default=0)
flat = models.ForeignKey(Flat)
created_date = models.DateField(auto_now_add= True)
created_time = models.TimeField(auto_now_add= True)
def __init__(self, *args, **kwargs):
super(Room, self).__init__(*args, **kwargs)
self.value_original = self.room_value
def save(self, **kwargs):
with transaction.atomic():
response = super(Room, self).save(**kwargs)
if self.value_original != self.room_value:
room_log = RoomLog()
room_log.room = self
room_log.room_value = self.value_original
room_log.save()
return response
class Meta:
ordering = ('room_name',)
def __unicode__(self):
return self.room_name
class RoomLog(models.Model):
room = models.ForeignKey(Room)
room_value = models.PositiveSmallIntegerField(default=0)
update_date = models.DateField(auto_now_add= True)
update_time = models.TimeField(auto_now_add= True)
def __str__(self):
return '%s | %s | %s' % (self.room, self.update_date, self.update_time)
list.append 更新列表 "in place" 和 returns None
例如:
my_list = [1, 2, 3]
new_list = my_list.append(4)
print my_list, new_list
[1, 2, 3, 4] None
只需删除赋值语句。
def choices(request):
form = RoomLogChoices()
form.fields['choice'].choices = list()
for i in Room.objects.all():
form.fields['choice'].choices.append(
(
i.room_name,
list(
(o.id, o.room_value) for o in RoomLog.objects.filter(room=i).order_by('room')
)
)
)
我收到以下错误
'/blocks/choices/ 处出现类型错误 NoneType' 对象不可迭代
我做错了什么?
查看
def choices(request):
form = RoomLogChoices()
form.fields['choice'].choices = list()
for i in Room.objects.all():
form.fields['choice'].choices = form.fields['choice'].choices.append(
(
i.room_name,
list(
(o.id, o.room_value) for o in RoomLog.objects.filter(room=i).order_by('room')
)
)
)
形式
class RoomLogChoices(forms.ModelForm):
choice = forms.ModelChoiceField(
queryset=RoomLog.objects.all().order_by('room__room_name', 'room_value')
)
class Meta:
model = RoomLog
fields = '__all__'
编辑: 以下是我要求的模型。出于这些原因,我使用了两个模型:Room 和 RoomLog。 RoomLog 仅用于跟踪 Room 值的更新历史。
型号
class Room(models.Model):
room_name = models.CharField(max_length= 10)
room_value = models.PositiveSmallIntegerField(default=0)
flat = models.ForeignKey(Flat)
created_date = models.DateField(auto_now_add= True)
created_time = models.TimeField(auto_now_add= True)
def __init__(self, *args, **kwargs):
super(Room, self).__init__(*args, **kwargs)
self.value_original = self.room_value
def save(self, **kwargs):
with transaction.atomic():
response = super(Room, self).save(**kwargs)
if self.value_original != self.room_value:
room_log = RoomLog()
room_log.room = self
room_log.room_value = self.value_original
room_log.save()
return response
class Meta:
ordering = ('room_name',)
def __unicode__(self):
return self.room_name
class RoomLog(models.Model):
room = models.ForeignKey(Room)
room_value = models.PositiveSmallIntegerField(default=0)
update_date = models.DateField(auto_now_add= True)
update_time = models.TimeField(auto_now_add= True)
def __str__(self):
return '%s | %s | %s' % (self.room, self.update_date, self.update_time)
list.append 更新列表 "in place" 和 returns None
例如:
my_list = [1, 2, 3]
new_list = my_list.append(4)
print my_list, new_list
[1, 2, 3, 4] None
只需删除赋值语句。
def choices(request):
form = RoomLogChoices()
form.fields['choice'].choices = list()
for i in Room.objects.all():
form.fields['choice'].choices.append(
(
i.room_name,
list(
(o.id, o.room_value) for o in RoomLog.objects.filter(room=i).order_by('room')
)
)
)