我有一个django模型,我使用自然键:
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
当我尝试使用以下命令序列化StudentAcademicPrograms
时,它会抛出错误。
>>> 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'
我哪里错了?
答案 0 :(得分:2)
根据documentation,use_natural_keys
是&#34;分裂&#34; Django 1.7版本中的use_natural_foreign_keys
和use_natural_primary_keys
目前还不是稳定版本。
您可以upgrade to django development version或使用use_natural_keys
关键字。