Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从外部API检索的模型的查询集_Python_Django_Api_Models - Fatal编程技术网

Python 从外部API检索的模型的查询集

Python 从外部API检索的模型的查询集,python,django,api,models,Python,Django,Api,Models,我目前正在开发一个web应用程序,它使用Amazon产品API获取书籍信息。我使用的图书模型只包含ASIN amazon标识码,看起来有点像这样: class Book(models.Model): asin = models.CharField(max_length-10, unique=True) def retrieve(self, **kwargs): kwargs['ItemId'] = self.asin self.xml = amaz

我目前正在开发一个web应用程序,它使用Amazon产品API获取书籍信息。我使用的图书模型只包含ASIN amazon标识码,看起来有点像这样:

class Book(models.Model):
    asin = models.CharField(max_length-10, unique=True)
    def retrieve(self, **kwargs):
        kwargs['ItemId'] = self.asin
        self.xml = amazon_lookup(**kwargs) # returns BeautifulSoup
    @property
    def title(self):
        try:
            return self.xml.ItemAttriibutes.Author.text
        except AttributeError: # happens when xml has not been populated
            return None
    ...


class BookManager(models.Manager):
    def retrieve(self, **kwargs):
        kwargs['SearchIndex'] = 'Books'
        book_search = amazon_search(**kwargs)
        books = []
        for item in book_search:
            book = self.get_or_create(asin=item.ASIN.text)[0]
            book.xml = item
            books.append(book)
        return books
然后我就可以用

b = Book.objects.retrieve(Keywords="foo bar")
b.retrieve(ResponseGroup="Images,ItemAttributes,...")
t = b.title
这对于测试来说已经足够好了,但是我想要一个更健壮的系统供将来使用。

我真正想做的是能够使用查询集执行搜索,以便可以缓存频繁访问的结果。目前,对书籍细节视图的每个请求都会创建一个新的AmazonAPI调用。如果所有的API调用都在查询集中与数据库调用一起处理,我的工作就会容易得多。不幸的是,我发现Django查询集文档非常隐晦,而且在定制方面缺乏。这肯定不是一个罕见的用例

有谁能提供处理此类问题的惯用方法,或者提供有关此主题的好资源吗?