我是Django的初學者,遇到ImportError: cannot import name simplejson
錯誤。 Here,有人建議更新extdirect的providers.py來導入json而不是simplejson,但我不知道經過長時間的網上衝浪如何做到這一點,我不知道什麼是extdirect?最終,我的目標是將ORM查詢結果集轉換爲json並將其傳遞給客戶端。Django - ImportError:無法導入名稱simplejson
下面是代碼:
from django.core import serializers
vendors_json = AgreementVendors.objects.select_related('agreement_unique').all()
return serializers.serialize('json', [x.agreement_unique for x in vendors_json])
型號:
class AgreementUnique(models.Model):
agreement_number = models.CharField(max_length=45, blank=True, null=True)
sign_date = models.DateTimeField(default=timezone.now)
cities = models.ForeignKey('address.Cities', on_delete=models.CASCADE)
sale_point = models.ForeignKey('legal_entity.SalePoint', on_delete=models.CASCADE)
sale_point_representative = models.ForeignKey('employee.Employees', on_delete=models.CASCADE)
partner_base = models.ForeignKey('PartnershipBase', on_delete=models.CASCADE)
agreement_from = models.DateField()
agreement_to = models.DateField()
blacknwhite = models.NullBooleanField()
prolongable = models.NullBooleanField()
class Meta:
managed = False
db_table = 'agreement_unique'
def __unicode__(self):
try:
return smart_unicode(AgreementEmployees.objects.get(agreement_unique=self.id).person_unique)
except AgreementEmployees.DoesNotExist:
return smart_unicode(self.agreement_number)
class AgreementVendors(models.Model):
agreement_unique = models.ForeignKey(AgreementUnique, on_delete=models.CASCADE)
vendors = models.ForeignKey('vendor.Vendors', on_delete=models.CASCADE)
return_percentage = models.FloatField()
payment_method = models.ForeignKey('additional.PaymentMethod', on_delete=models.CASCADE)
vendor_representative = models.ForeignKey('person.PersonUnique', on_delete=models.CASCADE)
days_of_payment = models.IntegerField()
class Meta:
managed = False
db_table = 'agreement_vendors'
def __unicode__(self):
return smart_unicode(self.vendors)
而且回溯:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 148, in get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 146, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\edgar\Desktop\Project\djprj\vendor\views.py", line 153, in create_new_vendor_agreement
return serializers.serialize('json', [x.agreement_unique for x in vendors_json])
File "C:\Python27\lib\site-packages\django\core\serializers\__init__.py", line 128, in serialize
s = get_serializer(format)()
File "C:\Python27\lib\site-packages\django\core\serializers\__init__.py", line 51, in __call__
raise self.exception
ImportError: cannot import name simplejson
而且,奇怪的是,我不能跨來嘗試通過項目範圍內的Ctrl + F進行搜索時發生simplejson單擊事件
您使用的是哪個版本的Django? – Alasdair
這是Django 1.9 –
我不知道Django升級是否失敗。 Django很久沒有使用'simplejson'了。你可以檢查'C:\ Python27 \ lib \ site-packages \ django \ core \ serializers \ json.py',看它是否包含'from django.utils import simplejson'這行嗎? – Alasdair