1

I am a beginner with Django and I've come across a ImportError: cannot import name simplejson error. Here, someone suggested updating extdirect's providers.py to import json instead of simplejson, but I have no idea after long hours of surfing net how to do this, and I can't figure out what is extdirect? Ultimately, my goal is to turn ORM query result set into json and pass it to client-side.

Here is the code:

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])

The model:

  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)

And the traceback:

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

Also, it is strange that I can't come across a single occurrence of simplejson when trying to search it via Ctrl+F project-wide

Community
  • 1
  • 1
Edgar Navasardyan
  • 4,261
  • 8
  • 58
  • 121
  • Which version of Django are you using? – Alasdair May 12 '16 at 08:36
  • 1
    I wonder if a Django upgrade might have failed. Django hasn't used `simplejson` for a long time. Can you check `C:\Python27\lib\site-packages\django\core\serializers\json.py`, and see whether it contains the line `from django.utils import simplejson`? – Alasdair May 12 '16 at 08:49
  • quite sure it's Django 1.9? pip freeze? if it is pleaes try reinstalling – e4c5 May 12 '16 at 09:30
  • 1
    It looks like the Django code that loads the serializers is hiding where the exception is coming from. You could use [pdb](https://docs.python.org/3/library/pdb.html) to step through the code and find the import which is failing. – Alasdair May 12 '16 at 09:41

1 Answers1

1

Check if

SERIALIZATION_MODULES

is inside your settings.py. It may override the default json serializer.

Irvan
  • 439
  • 4
  • 19