1
I have the following models -
class Jobdtl(models.Model):
jobdtl_id = models.IntegerField(primary_key=True)
jobdtl_cmd = models.TextField(blank=True)
jobdtl_envfile = models.TextField(blank=True)
jobdtl_retnsn = models.SmallIntegerField(blank=True, null=True)
jobdtl_allowadhoc = models.CharField(max_length=1, blank=True)
jobdtl_waitop = models.CharField(max_length=1, blank=True)
class Meta:
managed = False
db_table = 'jobdtl'
class Jobmst(models.Model):
jobmst_id = models.IntegerField(primary_key=True)
jobmst_prntid = models.IntegerField(blank=True, null=True)
jobmst_active = models.CharField(max_length=1, blank=True)
jobmst_evntoffset = models.SmallIntegerField(blank=True, null=True)
jobmst_name = models.TextField(blank=True)
jobmst_owner = models.IntegerField(blank=True, null=True)
jobmst_crttm = models.DateTimeField()
jobdtl_id = models.ForeignKey('Jobdtl', db_column='jobdtl_id', related_name='mstdtl', blank=True, null=True)
jobmst_lstchgtm = models.DateTimeField(blank=True, null=True)
def __unicode__(self):
return self.jobmst_name
class Meta:
managed = False
db_table = 'jobmst'
我試圖產生類似於我們這裏有一個JSON - 在這裏使用的步驟Django的REST框架嵌套關係
http://stackoverflow.com/questions/19709101/django-rest-framework-multiple-models
-
http://stackoverflow.com/questions/16793608/how-to-write-a-django-rest-framework-serializer-field-to-merge-data-from-gener
這些都是我的串行 -
class JobmstSerializer(serializers.ModelSerializer):
class Meta:
model = Jobmst
class JobdtlSerializer(serializers.ModelSerializer):
jobmst_id = JobmstSerializer(many=True)
class Meta:
model = Jobdtl
這是我的看法
class ResultsList(ListAPIView):
def list(self, request, *args, **kwargs):
jobmstquery = Jobmst.objects.using('Admiral').all()
jobdtlquery = Jobdtl.objects.using('Admiral').all()
results = list()
entries = list(chain(jobmstquery, jobdtlquery)) # combine the two querysets
for entry in entries:
type = entry.__class__.__name__.lower() # 'jobmst', 'jobdtl'
if isinstance(entry, Jobmst):
serializer = JobmstSerializer(entry)
dictionary = {'type': jobmst, 'jobmst_id': jobmst_id, 'jobmst_type': jobmst_type, 'jobmst_prntid': jobmst_prntid, 'jobmst_active': jobmst_active, 'evntmst_id': evntmst_id, 'jobmst_evntoffset': jobmst_evntoffset, 'jobmst_name': jobmst_name, 'jobmst_mode': jobmst_mode, 'jobmst_owner': jobmst_owner, 'jobmst_desc': jobmst_desc, 'jobmst_crttm': jobmst_crttm, 'jobdtl_id': jobdtl_id, 'jobmst_lstchgtm': jobmst_lstchgtm}
if isinstance(entry, Jobdtl):
serializer = JobdtlSerializer(entry)
dictionary = {'type': jobdtl, 'jobdtl_id': jobdtl, 'jobdtl_cmd': jobdtl_cmd, 'jobdtl_envfile': jobdtl_envfile, 'jobdtl_retnsn': jobdtl_retnsn, 'jobdtl_allowadhoc': jobdtl_allowadhoc, 'jobdtl_waitop': jobdtl_waitop}
results.append(dictionary)
return Response(results)
我配合它通過我的網址 -
urlpatterns = patterns('TidalDEV.views',
url(r'^TidalDEV/$', 'ResultsList'),
url(r'^TidalDEV/(?P<pk>[0-9]+)/$', 'ResultsList'),
)
但是,當我打我的網址在http://localhost/TidalDEV/
或http://localhost/TidalDEV/50244/
我得到一個錯誤砰 -
Exception Type: TypeError
Exception Value:
__init__() takes 1 positional argument but 2 were given
Exception Location: D:\Python33\lib\site-packages\django\core\handlers\base.py in get_response, line 114
如果可能,請提供完整的堆棧跟蹤。 – mariodev
其實我覺得我找到了解決方案(還在調查中)。我已經鏈接了兩張表,查看更多鏈接。 – whoisearth