django brookie - “NoneType”对象没有属性“状态”;
前几天偶然发现了 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)
我尝试了各种方法来测试只读字段之前对象的存在性。如果发票处于非开发状态,则返回只读字段 (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'
Bumped into the Brookie app the other day and had a look around. Very nice for creating invoice PDF's on save.
However, I get this error on creating a new invoice. I know that the error arises because there is no object and therefore it cannot have an attribute.
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)
I've tried various ways to test the existance of the object before readonly fields. Readonly fields are returned if the invoice is in a state othehr than indevelopment (1)
How can I ignore the get_readonly_fields function if the object is new?
I've also tried adding a default=1 to the model, testing for the existance of an obj before trying to access the status attibute to no sucess.
Thanks in advance
## EDIT ##
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,即使在
obj
为None
的情况下,您也尝试评估status
属性,这显然是可接受的情况。要修复它,您可以将其更改
为:
The problem is that you're trying to evaluate the
status
attribute even in the case whereobj
isNone
, which is apparently an acceptable case.To fix it, you could change this:
To this: