我正在尝试使用inlineformset在我的网页上显示特定条件的现有条目。以下是我视图中的行
TimeFormSet = inlineformset_factory(TimeSheet, Time, form=TimeForm, extra=1)
person_timesheets = Time.objects.filter(timesheet_id=timesheet)
print person_timesheets.count()
time_formset = TimeFormSet(instance=person_timesheets)
我不明白为什么会造成这种情况
'QuerySet'对象没有属性'timesheet_id'
print person_timesheets.count()向我的控制台返回一个数字没问题...我必须在这里使用实例错误。
以下是我的模特
class Time(models.Model):
description = models.CharField(max_length=150)
timesheet_id = models.ForeignKey(TimeSheet, null=True, related_name="time_set")
def __unicode__ (self):
return self.description
class TimeSheet(models.Model):
start_date = models.DateField()
status = models.CharField(max_length= 50, default="Open", choices=STATUS_CHOICES)
当我在我的视图中保存我的时间时,我将timesheet_id字段设置为相关TimeSheet的Django分配ID(不确定这是否是一种混乱的方式来执行此操作...)。
def week(request, timesheet_id):
# navigated from previous timesheet view
timesheet = TimeSheet.objects.get(pk=timesheet_id)
TimeFormSet = inlineformset_factory(TimeSheet, Time, form=TimeForm, extra=1)
person_timesheets = Time.objects.filter(timesheet_id=timesheet)
print person_timesheets.count()
time_formset = TimeFormSet(instance=person_timesheets)
if request.method == 'POST':
time_formset = TimeFormSet(request.POST)
#check validity first
if time_formset.is_valid():
for form in time_formset:
form.timesheet_id = timesheet
form.save()
context = {
"timesheet": timesheet,
"time_formset": time_formset,
}
return render(request, 'tande/week.html', context)
编辑:追溯
Internal Server Error: /tande/1/timesheet/week/
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/Users/ruthyoung/Documents/code/prodman/tande/views.py", line 344, in week
time_formset = TimeFormSet(instance=person_timesheets.timesheet_id)
AttributeError: 'QuerySet' object has no attribute 'timesheet_id'
帮助!?
答案 0 :(得分:2)
将相关字段命名为 id name date Key
1 charles 2004-05-07 1001
2 CON 2004-05-07 1001
3 Virginia 2006-09-08 1001
4 MART 2012-01-03 1001
5 McDonalds 2013-12-30 1001
OUTPUT
id old_name name date Key
1 NULL charles 2004-05-07 1001
2 charles CON 2004-05-07 1001
3 CON Virginia 2006-09-08 1001
4 Virginia MART 2012-01-03 1001
5 MART McDonalds 2013-12-30 1001
并不是一个好习惯。只需related_name_id
。这是因为将来,当您希望获得相关字段的related_name
时,以这种方式写id
代替filter(related_name__id=4)
更具可读性
您在filter(related_name_id__id=4)
formset工厂内传递了Time QuerySet
而不是TimeSheet
个实例。更改为TimeFormSet
。
如果您想显示与其相关的所有现有对象,那么只需time_formset = TimeFormSet(instance=timesheet)
答案 1 :(得分:0)
请记住,当您使用Time.objects.filter(timesheet_id=timesheet)
时,您将获得一个QuerySet(模型中的实例列表,在本例中为Time
模型)
执行此操作时:instance=person_timesheets
将实例化完整查询集(查询返回的所有实例)而不是特定实例,因此会抛出错误'QuerySet' object has no attribute 'timesheet_id'
尝试:
person_timesheets = Time.objects.get(id=timesheet_id)
time_formset = TimeFormSet(instance=person_timesheets)