尝试 post 数据时出现 Django 1.11 错误
Django 1.11 error while trying to post data
我是 Django 1.11 LTS 的新手,很长一段时间以来我一直在努力解决这个错误。这是我发生错误的代码:
model.py:
name = models.CharField(db_column="name", db_index=True, max_length=128)
description = models.TextField(db_column="description", null=True, blank=True)
created = models.DateTimeField(db_column="created", auto_now_add=True, blank=True)
updated = models.DateTimeField(db_column="updated", auto_now=True, null=True)
active = models.BooleanField(db_column="active", default=True)
customer = models.ForeignKey(Customer, db_column="customer_id")
class Meta(object):
db_table="customer_build"
unique_together = ("name", "customer")
def __unicode__(self):
return u"%s [%s]" % (self.name, self.customer)
def get(self,row,customer):
build_name = row['build']
return self._default_manager.filter(name = build_name, customer_id = customer.id).first()
def add(self,row):
pass
Views.py块:
for row in others:
rack_name = row['rack']
build = Build().get(row,customer)
try:
rack = Rack().get(row,customer)
except Exception as E:
msg = {'exception': str(E), 'where':'Non-server device portmap creation', 'doing_what': 'Rack with name {} does not exist in build {}'.format(rack_name,build.name),
'current_row': row, 'status': 417}
log_it('An error occurred: {}'.format(msg))
return JsonResponse(msg, status = 417)
错误回溯:
File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./customers/views.py", line 2513, in create_rack
add_rack_status = add_rack(customer, csv_file)
File "./customers/views.py", line 1803, in add_rack
build = Build().get(row,customer)
File "./customers/models.py", line 69, in get
return self._default_manager.filter(name = build_name, customer_id = customer.id).first()
AttributeError: 'Build' object has no attribute '_default_manager'
我正在尝试了解问题以便解决它。
提前致谢。
此致,
巴拉斯
尝试更改此行:
return self._default_manager.filter(name = build_name, customer_id = customer.id).first()
对此:
return self.__class__._default_manager.filter(name = build_name, customer_id = customer.id).first()
根据 Django 1.10,_default_manager 已弃用,所以我做了 return Build.objects.filter(name = build_name, customer_id = customer.id).first() and it solved it.
我是 Django 1.11 LTS 的新手,很长一段时间以来我一直在努力解决这个错误。这是我发生错误的代码:
model.py:
name = models.CharField(db_column="name", db_index=True, max_length=128)
description = models.TextField(db_column="description", null=True, blank=True)
created = models.DateTimeField(db_column="created", auto_now_add=True, blank=True)
updated = models.DateTimeField(db_column="updated", auto_now=True, null=True)
active = models.BooleanField(db_column="active", default=True)
customer = models.ForeignKey(Customer, db_column="customer_id")
class Meta(object):
db_table="customer_build"
unique_together = ("name", "customer")
def __unicode__(self):
return u"%s [%s]" % (self.name, self.customer)
def get(self,row,customer):
build_name = row['build']
return self._default_manager.filter(name = build_name, customer_id = customer.id).first()
def add(self,row):
pass
Views.py块:
for row in others:
rack_name = row['rack']
build = Build().get(row,customer)
try:
rack = Rack().get(row,customer)
except Exception as E:
msg = {'exception': str(E), 'where':'Non-server device portmap creation', 'doing_what': 'Rack with name {} does not exist in build {}'.format(rack_name,build.name),
'current_row': row, 'status': 417}
log_it('An error occurred: {}'.format(msg))
return JsonResponse(msg, status = 417)
错误回溯:
File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./customers/views.py", line 2513, in create_rack
add_rack_status = add_rack(customer, csv_file)
File "./customers/views.py", line 1803, in add_rack
build = Build().get(row,customer)
File "./customers/models.py", line 69, in get
return self._default_manager.filter(name = build_name, customer_id = customer.id).first()
AttributeError: 'Build' object has no attribute '_default_manager'
我正在尝试了解问题以便解决它。
提前致谢。
此致, 巴拉斯
尝试更改此行:
return self._default_manager.filter(name = build_name, customer_id = customer.id).first()
对此:
return self.__class__._default_manager.filter(name = build_name, customer_id = customer.id).first()
根据 Django 1.10,_default_manager 已弃用,所以我做了 return Build.objects.filter(name = build_name, customer_id = customer.id).first() and it solved it.