我尝试将数据导入db解析textarea,但我得到了这个错误:
'NoneType' object has no attribute '_deferred'
。我做错了什么?感谢。
# admin.py
class AppleAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
if obj is None:
return AddApple
super(AppleAdmin, self).get_form(request, obj=None, **kwargs)
def save_model(self, request, obj, form, change):
return
def save_related(self, request, form, formsets, change):
return
admin.site.register(Apple, AppleAdmin)
# models.py
class Apple(models.Model):
banana = models.CharField(max_length=255)
def add_banana(self, banana):
self.banana = banana.replace('apple', 'banana')
# forms.py
class AddApple(forms.ModelForm):
class Meta:
model = Apple
fields = ('apples',)
apples = forms.CharField(widget=forms.Textarea)
def save(self, commit=True):
apples = []
for x in self.cleaned_data.get("apples").splitlines():
if not x.strip():
continue
a = Apple()
a.add_banana(x.strip())
apples.append(a)
Apple.objects.bulk_create(apples)
super(AddApple, self).save(commit=commit)
所以,使用这段代码我得到了这个错误+追溯:
Traceback:
File "|site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "|site-packages\django\contrib\admin\options.py" in wrapper
616. return self.admin_site.admin_view(view)(*args, **kwargs)
File "|site-packages\django\utils\decorators.py" in _wrapped_view
110. response = view_func(request, *args, **kwargs)
File "|site-packages\django\views\decorators\cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "|site-packages\django\contrib\admin\sites.py" in inner
233. return view(request, *args, **kwargs)
File "|site-packages\django\contrib\admin\options.py" in add_view
1516. return self.changeform_view(request, None, form_url, extra_context)
File "|site-packages\django\utils\decorators.py" in _wrapper
34. return bound_func(*args, **kwargs)
File "|site-packages\django\utils\decorators.py" in _wrapped_view
110. response = view_func(request, *args, **kwargs)
File "|site-packages\django\utils\decorators.py" in bound_func
30. return func.__get__(self, type(self))(*args2, **kwargs2)
File "|contextlib.py" in inner
30. return func(*args, **kwds)
File "|site-packages\django\contrib\admin\options.py" in changeform_view
1470. self.log_addition(request, new_object)
File "|site-packages\django\contrib\admin\options.py" in log_addition
808. content_type_id=get_content_type_for_model(object).pk,
File "|site-packages\django\contrib\admin\options.py" in get_content_type_for_model
65. return ContentType.objects.get_for_model(obj, for_concrete_model=False)
File "|site-packages\django\contrib\contenttypes\models.py" in get_for_model
53. opts = self._get_opts(model, for_concrete_model)
File "|site-packages\django\contrib\contenttypes\models.py" in _get_opts
31. elif model._deferred:
Exception Type: AttributeError at /apples/apple/add/
Exception Value: 'NoneType' object has no attribute '_deferred'
答案 0 :(得分:2)
我不明白为什么你将管理类上的那些方法重写为return
- 你实际上需要在那里调用save方法。
但是,您直接问题的根源在于您没有从表单的save
方法返回该对象。它应该是:
return super(AddApple, self).save(commit=commit)
答案 1 :(得分:0)
我认为你有问题:
def get_form(self, request, obj=None, **kwargs):
if obj is None:
return AddApple
super(AppleAdmin, self).get_form(request, obj=None, **kwargs)
应该是:
def get_form(self, request, obj=None, **kwargs):
if obj is None:
return AddApple
return super(AppleAdmin, self).get_form(request, obj, **kwargs)
注意return
,而不是强制obj=None