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 -
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
答案 0 :(得分:1)
我最终做了以下工作 - 它起作用了 -
class JobdtlSerializer(serializers.ModelSerializer):
class Meta:
model = Jobdtl
class JobmstSerializer(serializers.ModelSerializer):
jobdtl_id = JobdtlSerializer()
class Meta:
model = Jobmst
然后连接默认视图的变体 -
@csrf_exempt
def jobmst_list(request):
"""
List all code snippets, or create a new snippet.
"""
if request.method == 'GET':
DEV = Jobmst.objects.using('AdmiralDEV').all()
serializer = JobmstSerializer(DEV, many=True)
return XMLResponse(serializer.data)
elif request.method == 'POST':
data = XMLParser().parse(request)
serializer = JobmstSerializer(data=data)
if serializer.is_valid():
serializer.save()
return XMLResponse(serializer.data, status=201)
else:
return XMLResponse(serializer.errors, status=400)
@csrf_exempt
def jobmst_detail(request, pk):
"""
Retrieve, update or delete a code snippet.
"""
try:
DEV = Jobmst.objects.using('AdmiralDEV').get(jobmst_id=pk)
except Jobmst.DoesNotExist:
return HttpResponse(status=404)
if request.method == 'GET':
serializer = JobmstSerializer(DEV)
return XMLResponse(serializer.data)
elif request.method == 'PUT':
data = XMLParser().parse(request)
serializer = JobmstSerializer(DEV, data=data)
if serializer.is_valid():
serializer.save()
return XMLResponse(serializer.data)
else:
return XMLResponse(serializer.errors, status=400)
elif request.method == 'DELETE':
DEV.delete()
return HttpResponse(status=204)
这让我可以从两个独立的模型中获得。它没有格式化和丑陋但证明它可以完成。现在需要弄清楚如何链接超过这两个模型(我最少有5个)。