前几天撞到了Brookie应用程序并浏览了一下。非常适合在保存时创建发票PDF。
但是,我在创建新发票时遇到此错误。我知道错误的产生是因为没有对象,因此它没有属性。
class InvoiceAdmin(admin.ModelAdmin):
list_display = ('client', 'status', 'date', total_monetized, is_expired, pdf_invoice)
list_filter = ('status', 'client')
exclude = ('invoice_no',)
ordering = ('id', )
search_fields = ['client__company', ]
readonly_fields = ()
inlines = [ItemInline,]
class Media:
js = ('http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js', 'brookie/js/brookie.js')
def get_readonly_fields(self, request, obj=None):
readonly = super(InvoiceAdmin, self).get_readonly_fields(request, obj)
# if the invoice is send you can no longer alter it
if obj.status in br_settings.INVOICE_FINISH_STATUS:
readonly = ('invoice_id', 'client', 'date', 'currency', 'tax', 'hourly_rate')
return readonly
def save_model(self, request, obj, form, change):
obj.save()
if obj.status in br_settings.INVOICE_FINISH_STATUS:
# Set the invoice id
if not obj.invoice_no:
invoice_list = Invoice.objects.filter(invoice_no__isnull=False).order_by('-invoice_no')
try:
invoice = invoice_list[0]
except:
# There are no numbered invoices
invoice_no = getattr(br_settings, 'INVOICE_START_NUMBER', 1)
else:
invoice_no = invoice.invoice_no + 1
obj.invoice_no = invoice_no
obj.save()
# Generate the pdf for this invoice
context_dict = {'invoice': obj,
'client': obj.client,
'items': obj.items.all(),}
generate_pdf(obj.invoice_id, context_dict, "brookie/invoice_%s_pdf.html" % obj.currency, save=True)
我尝试过各种方法在readonly字段之前测试对象的存在。如果发票处于其他状态而非开发状态(1)
,则返回只读字段如果对象是新对象,如何忽略get_readonly_fields函数? 我还尝试在模型中添加一个default = 1,在尝试访问状态属性之前测试obj的存在性。
提前致谢
##编辑##
Traceback:
File "C:\Python26\lib\site-packages\django\core\handlers\base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python26\lib\site-packages\django\contrib\admin\options.py" in wrapper
239. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python26\lib\site-packages\django\utils\decorators.py" in _wrapped_view
76. response = view_func(request, *args, **kwargs)
File "C:\Python26\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
69. response = view_func(request, *args, **kwargs)
File "C:\Python26\lib\site-packages\django\contrib\admin\sites.py" in inner
190. return view(request, *args, **kwargs)
File "C:\Python26\lib\site-packages\django\utils\decorators.py" in _wrapper
21. return decorator(bound_func)(*args, **kwargs)
File "C:\Python26\lib\site-packages\django\utils\decorators.py" in _wrapped_view
76. response = view_func(request, *args, **kwargs)
File "C:\Python26\lib\site-packages\django\utils\decorators.py" in bound_func
17. return func(self, *args2, **kwargs2)
File "C:\Python26\lib\site-packages\django\db\transaction.py" in _commit_on_success
299. res = func(*args, **kw)
File "C:\Python26\lib\site-packages\django\contrib\admin\options.py" in add_view
773. ModelForm = self.get_form(request)
File "C:\Python26\lib\site-packages\django\contrib\admin\options.py" in get_form
356. exclude.extend(self.get_readonly_fields(request, obj))
File "C:\Sites\media\bread-and-pepper-django-brookie-a1a8102\brookie\admin.py" in get_readonly_fields
124. if obj.status in br_settings.INVOICE_FINISH_STATUS:
Exception Type: AttributeError at /admin/brookie/invoice/add/
Exception Value: 'NoneType' object has no attribute 'status'
答案 0 :(得分:1)
问题在于,即使在status
为obj
的情况下,您也在尝试评估None
属性,这显然是可接受的情况。
要解决此问题,您可以更改此内容:
# if the invoice is send you can no longer alter it
if obj.status in br_settings.INVOICE_FINISH_STATUS:
readonly = ('invoice_id', 'client', 'date', 'currency', 'tax', 'hourly_rate')
对此:
# if the invoice is send you can no longer alter it
if obj and obj.status in br_settings.INVOICE_FINISH_STATUS:
readonly = ('invoice_id', 'client', 'date', 'currency', 'tax', 'hourly_rate')