tastypie使用itertools链

时间:2013-12-17 18:41:40

标签: django tastypie

面向未来的人们。我已经废弃了tastypie,并且已经成功使用了Django-Rest-Framework和itertools,正如在这个问题的批准答案中所建议的那样。

我有2个模型,我想将结果链接起来,因为它们是由FK关联的。

我已经在我的API.py中创建了类,如下所示 -

class JobmstResource(ModelResource):
    jobdtl_id = fields.ForeignKey(JobdtlResource, 'jobdtl_id')
    class Meta:
        jobdtlquery = Jobdtl.objects.using('Admiral').all()
        jobmstquery = Jobmst.objects.using('Admiral').all()
        queryset = chain(jobdtlquery, jobmstquery)
        resource_name = 'Jobmst'

然后我从调用ID的URL(两个查询中的公共ID)中点击我的api。

http://localhost:8080/api/jobmst/3296/?format=xml

当我运行它时,它无法抱怨 -

'itertools.chain'对象没有属性'model'

似乎django没有干净清晰的方法来进行内部连接,当你想要超过1个模型的结果时这么好奇的地方是什么?我应该以不同的方式进行攻击吗?

如果我从简单的HTML查看查询,它会显示结果,但会将值分成2个模型字段和所有。

我希望值显示为来自1个模型。

以下是有问题的2个模型的models.py -

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)
    jobdtl_fromdt = models.DateTimeField(blank=True, null=True)
    jobdtl_untildt = models.DateTimeField(blank=True, null=True)
    jobdtl_fromtm = models.DateTimeField(blank=True, null=True)
    jobdtl_untiltm = models.DateTimeField(blank=True, null=True)
    jobdtl_proxy = models.ForeignKey('Usrmst', db_column='jobdtl_proxy', related_name='Jobdtl_jobdtl_proxy',blank=True, null=True)
    jobdtl_proxy2 = models.ForeignKey('Usrmst', db_column='jobdtl_proxy2', related_name='Jobdtl_jobdtl_proxy2',blank=True, null=True)
    jobdtl_interval = models.SmallIntegerField(blank=True, null=True)
    jobdtl_intervalcnt = models.SmallIntegerField(blank=True, null=True)
    jobdtl_unit = models.CharField(max_length=1, blank=True)
    jobdtl_duration = models.IntegerField(blank=True, null=True)
    jobdtl_concur = models.SmallIntegerField(blank=True, null=True)
    jobdtl_priority = models.SmallIntegerField(blank=True, null=True)
    jobdtl_minrun = models.IntegerField(blank=True, null=True)
    jobdtl_maxrun = models.IntegerField(blank=True, null=True)
    jobdtl_failalarm = models.CharField(max_length=1, blank=True)
    nodmst_id = models.ForeignKey('Nodmst', db_column='Nodmst_id', related_name='Jobdtl_Nodmst_id', blank=True, null=True)
    nodlstmst_id = models.ForeignKey('Nodlstms', db_column='Nodlstmst_id', related_name='Jobdtl_Nodlstmst_id',blank=True, null=True)
    jobdtl_inhevent = models.CharField(max_length=1, blank=True)
    jobdtl_inhoptions = models.CharField(max_length=1, blank=True)
    jobdtl_inhagent = models.CharField(max_length=1, blank=True)
    jobdtl_inhrepeat = models.CharField(max_length=1, blank=True)
    jobdtl_inhtime = models.CharField(max_length=1, blank=True)
    jobdtl_timewin = models.SmallIntegerField(blank=True, null=True)
    jobdtl_saveoutput = models.CharField(max_length=1, blank=True)
    jobdtl_outputname = models.TextField(blank=True)
    jobdtl_trackmethod = models.SmallIntegerField(blank=True, null=True)
    jobdtl_trackcmd = models.TextField(blank=True)
    jobdtl_deplogic = models.SmallIntegerField(blank=True, null=True)
    jobdtl_rerun = models.CharField(max_length=1, blank=True)
    jobdtl_params = models.TextField(blank=True) # This field type is a guess.
    jobdtl_sapcount = models.IntegerField(blank=True, null=True)
    jobdtl_normalexit = models.SmallIntegerField(blank=True, null=True)
    jobdtl_normalrange = models.SmallIntegerField(blank=True, null=True)
    jobdtl_normalop = models.SmallIntegerField(blank=True, null=True)
    jobdtl_deprerun = models.CharField(max_length=1, blank=True)
    jobdtl_carryover = models.SmallIntegerField(blank=True, null=True)
    jobdtl_psjob = models.IntegerField(blank=True, null=True)
    jobdtl_savelogonly = models.CharField(max_length=1, blank=True)
    jobdtl_trxid = models.IntegerField(blank=True, null=True)
    jobdtl_rerunok = models.CharField(max_length=1, blank=True)
    jobdtl_workdir = models.TextField(blank=True)
    jobdtl_extinfo = models.TextField(blank=True) # This field type is a guess.
    servicemst_id = models.ForeignKey('Servicemst', db_column='Servicemst_id', blank=True, null=True)
    jobdtl_estmethod = models.SmallIntegerField(blank=True, null=True)
    jobdtl_nearoutage = models.SmallIntegerField(blank=True, null=True)
    jobdtl_trackcl = models.CharField(max_length=1, blank=True)
    jobdtl_statuscl = models.CharField(max_length=1, blank=True)
    jobdtl_abrtonclderr = models.CharField(max_length=1, blank=True)
    jobdtl_estdurexclude = models.IntegerField(blank=True, null=True)
    class Meta:
        managed = False
        db_table = 'jobdtl'

class Jobmst(models.Model):
    jobmst_id = models.IntegerField(primary_key=True)
    jobmst_type = models.SmallIntegerField()
    jobmst_prntid = TreeForeignKey('self', null=True, blank=True, related_name='children', db_column='jobmst_prntid')
    jobmst_active = models.CharField(max_length=1, blank=True)
    evntmst_id = models.IntegerField(blank=True, null=True)
    jobmst_evntoffset = models.SmallIntegerField(blank=True, null=True)
    jobmst_name = models.TextField(db_column='jobmst_name', blank=True)
    jobmst_mode = models.SmallIntegerField(blank=True, null=True)
    jobmst_owner = models.ForeignKey('Owner', db_column='jobmst_owner', related_name = 'Jobmst_Jobmst_owner', blank=True, null=True)
    jobmst_desc = models.TextField(blank=True) # This field type is a guess.
    jobmst_crttm = models.DateTimeField()
    jobdtl_id = models.ForeignKey('Jobdtl', db_column='jobdtl_id', blank=True, null=True)
    jobmst_lstchgtm = models.DateTimeField(blank=True, null=True)
    jobmst_runbook = models.TextField(blank=True) # This field type is a guess.
    jobcls_id = models.IntegerField(blank=True, null=True)
    jobmst_prntname = models.TextField(blank=True)
    jobmst_alias = models.CharField(max_length=10, blank=True)
    jobmst_dirty = models.CharField(max_length=1, blank=True)
    class MPTTMeta:
        order_insertion_by = ['jobmst_id']
    class Meta:
        managed = True
        db_table = 'jobmst'

2 个答案:

答案 0 :(得分:1)

您是否尝试过将itertools.chain转换为列表:

queryset = list(chain(jobdtlquery, jobmstquery))

更新您可以在同一模型上连接查询,如下所示:

queryset = query1 | query2

更新如错误消息所示,您的查询集必须只有一个model。您无法通过两种不同的模型实现目标。

这个问题似乎是dublicate

答案 1 :(得分:0)

因为ResourceModel允许REST客户端进行过滤,分页和排序的查询,然后触发相应的SQL查询,所以不可能只使用不能灵活的迭代器。

也许你可以破解可行的东西,但看起来最干净的东西是使用普通资源并添加你自己的逻辑。请参阅此答案Tastypie: How can I fill the resource without database?,该答案引用文档http://django-tastypie.readthedocs.org/en/latest/resources.html#why-resource-vs-modelresource

中的Resource VS ModelResource