Django - ImportError:无法导入名称simplejson

时间:2016-05-12 08:26:10

标签: python json django serialization simplejson

我是Django的初学者,我遇到ImportError: cannot import name simplejson错误。 Here,有人建议更新extdirect的providers.py来导入json而不是simplejson,但我不知道经过长时间的网上冲浪如何做到这一点,我无法弄清楚是什么是extdirect?最终,我的目标是将ORM查询结果集转换为json并将其传递给客户端。

以下是代码:

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

1 个答案:

答案 0 :(得分:1)

检查是否

SERIALIZATION_MODULES

位于settings.py内。它可能会覆盖默认的json序列化程序。