'str'对象没有属性'model'

时间:2019-07-16 10:46:56

标签: python django

@staff_member_required
@never_cache
def departures(request, fields=None):
    qs = Booking.objects.filter(status="PURCHASED").order_by=('departure_date')
    model = qs.model
    response = HttpResponse(mimetype='text/csv')
    response['Content-Disposition'] = 'attachment; filename=%s.csv' % slugify(model.__name__)
    writer = csv.writer(response)
    # Write headers to CSV file
    if fields:
        headers = fields
    else:
        headers = []
        for field in model._meta.fields:
            headers.append(field.name)
    writer.writerow(headers)
        # Write data to CSV file
    for obj in qs:
        row = []
        for field in headers:
            if field in headers:
                val = getattr(obj, field)
                if callable(val):
                    val = val()
                row.append(val)
        writer.writerow(row)
    return response

line model = qs.model引发错误'str' object has no attribute 'model'

但是我还有其他具有相同/相似代码且不会抛出错误的函数

if form.cleaned_data.has_key('departure_date') and form.cleaned_data['departure_date'] != '' and form.cleaned_data['departure_date'] != None:
    qs=Booking.objects.filter(departure_date__travel_date__exact=form.cleaned_data['departure_date']).filter(status="PURCHASED")
model = qs.model

所以我很惊讶抛出错误

1 个答案:

答案 0 :(得分:-1)

第4行的变量qs被分配了字符串departure_date,因此请尝试删除=,然后重试。