OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

django serialization on natural key error

  • Thread starter Thread starter Abhishek
  • Start date Start date
A

Abhishek

Guest
I have a django model where i use natural keys:

Code:
class AcademicProgramsManager(models.Manager):
    def get_by_natural_key(self, acad_program_id, program_title, required_credits):
        return self.get(acad_program_id = acad_program_id, program_title = program_title, required_credits = required_credits)

class AcademicPrograms(models.Model):

    objects = AcademicProgramsManager()
    acad_program_id = models.IntegerField(primary_key=True)
    acad_program_category = models.ForeignKey(AcademicProgramCategories)
    acad_program_type = models.ForeignKey(AcademicProgramTypes)
    acad_program_code = models.CharField(max_length=25)
    program_title = models.CharField(max_length=64)
    required_credits = models.IntegerField()
    min_gpa = models.FloatField()
    description = models.CharField(max_length=1000)

    def natural_key(self):
    return (self.acad_program_id, self.program_title, self.required_credits)

class StudentAcademicPrograms(models.Model):
    student = models.ForeignKey(Students)
    academic_program = models.ForeignKey(AcademicPrograms)
    credits_completed = models.IntegerField()
    academic_program_gpa = models.FloatField()
    primary_program = models.BooleanField()

    def __str__(self):
    return self.credits_completed

When i am trying to serialize the StudentAcademicPrograms with the following command, it is throwing an error.

Code:
>>> with open("datafile.json","w") as out:...     
        serialize("json", StudentAcademicPrograms.objects.all(), indent=2, use_natural_foreign_keys=True, use_natural_primary_keys=True)
    ... 
    Traceback (most recent call last):
      File "<console>", line 2, in <module>
      File "/home/abhishek/projects/texascompletes/local/lib/python2.7/site-packages/django/core/serializers/__init__.py", line 122, in serialize
        s.serialize(queryset, **options)
      File "/home/abhishek/projects/texascompletes/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 58, in serialize
        self.end_object(obj)
      File "/home/abhishek/projects/texascompletes/local/lib/python2.7/site-packages/django/core/serializers/json.py", line 52, in end_object
        cls=DjangoJSONEncoder, **self.json_kwargs)
      File "/usr/lib/python2.7/json/__init__.py", line 186, in dump
        default=default, sort_keys=sort_keys, **kw).iterencode(obj)
    TypeError: __init__() got an unexpected keyword argument 'use_natural_foreign_keys'

Where am i going wrong?
<p>I have a django model where i use natural keys:</p>

<pre><code>class AcademicProgramsManager(models.Manager):
def get_by_natural_key(self, acad_program_id, program_title, required_credits):
return self.get(acad_program_id = acad_program_id, program_title = program_title, required_credits = required_credits)

class AcademicPrograms(models.Model):

objects = AcademicProgramsManager()
acad_program_id = models.IntegerField(primary_key=True)
acad_program_category = models.ForeignKey(AcademicProgramCategories)
acad_program_type = models.ForeignKey(AcademicProgramTypes)
acad_program_code = models.CharField(max_length=25)
program_title = models.CharField(max_length=64)
required_credits = models.IntegerField()
min_gpa = models.FloatField()
description = models.CharField(max_length=1000)

def natural_key(self):
return (self.acad_program_id, self.program_title, self.required_credits)

class StudentAcademicPrograms(models.Model):
student = models.ForeignKey(Students)
academic_program = models.ForeignKey(AcademicPrograms)
credits_completed = models.IntegerField()
academic_program_gpa = models.FloatField()
primary_program = models.BooleanField()

def __str__(self):
return self.credits_completed
</code></pre>

<p>When i am trying to serialize the <code>StudentAcademicPrograms</code> with the following command, it is throwing an error.</p>

<pre><code>>>> with open("datafile.json","w") as out:...
serialize("json", StudentAcademicPrograms.objects.all(), indent=2, use_natural_foreign_keys=True, use_natural_primary_keys=True)
...
Traceback (most recent call last):
File "<console>", line 2, in <module>
File "/home/abhishek/projects/texascompletes/local/lib/python2.7/site-packages/django/core/serializers/__init__.py", line 122, in serialize
s.serialize(queryset, **options)
File "/home/abhishek/projects/texascompletes/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 58, in serialize
self.end_object(obj)
File "/home/abhishek/projects/texascompletes/local/lib/python2.7/site-packages/django/core/serializers/json.py", line 52, in end_object
cls=DjangoJSONEncoder, **self.json_kwargs)
File "/usr/lib/python2.7/json/__init__.py", line 186, in dump
default=default, sort_keys=sort_keys, **kw).iterencode(obj)
TypeError: __init__() got an unexpected keyword argument 'use_natural_foreign_keys'
</code></pre>

<p>Where am i going wrong?</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top