Django rest api问题:应包含“ querylist”属性,或覆盖“ get_querylist()”方法

时间:2019-05-29 20:19:12

标签: django python-3.x django-rest-framework django-views

这是我的序列化器类:

Traceback (most recent call last):
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 298, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: backgroundtasks_historicalnodesdata.history_date

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/background_task/tasks.py", line 43, in bg_runner
    func(*args, **kwargs)
  File "/home/django/copypaste/cleanup/backgroundtasks/tasks.py", line 243, in csvscraper
    foo.save()
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/models/base.py", line 718, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/models/base.py", line 758, in save_base
    update_fields=update_fields, raw=raw, using=using,
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 175, in send
    for receiver in self._live_receivers(sender)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 175, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/simple_history/models.py", line 459, in post_save
    self.create_historical_record(instance, created and "+" or "~", using=using)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/simple_history/models.py", line 501, in create_historical_record
    history_instance.save(using=using)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/models/base.py", line 718, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/models/base.py", line 748, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/models/base.py", line 831, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/models/base.py", line 869, in _do_insert
    using=using, raw=raw)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/models/query.py", line 1136, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1289, in execute_sql
    cursor.execute(sql, params)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/django/copypaste/cleanup/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 298, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: backgroundtasks_historicalnodesdata.history_date

这是我的观点

class JlistSerializers(serializers.ModelSerializer):
    class Meta:
        model = Jlist
        fields = ('id', 'name', 'news_channel', 'wiki', 'image', 'total_star', 'total_user')

我遇到了后续错误

class JlistView(ObjectMultipleModelAPIView):
    queryset = Jlist.objects.all()

    def get_queryset(self, *args, **kwargs):
        userId = self.kwargs.get('pk')
        queryset = [
            {'queryset': Jlist.objects.all(),
             'serializer_class': JlistSerializers},
            {'queryset': JStarList.objects.filter(userId=userId),
             'serializer_class': JStarList}
        ]
        return queryset

我使用相同的代码为其他序列化程序类创建api,但是在创建此api时出现错误。请帮助我找出这里的问题是什么?

1 个答案:

答案 0 :(得分:2)

您定义了get_queryset,但它应该是get_querylist。并删除queryset成员。

class JlistView(ObjectMultipleModelAPIView):

    def get_querylist(self, *args, **kwargs):
        userId = self.kwargs.get('pk')
        queryset = [
            {'queryset': Jlist.objects.all(),
             'serializer_class': JlistSerializers},
            {'queryset': JStarList.objects.filter(userId=userId),
             'serializer_class': JStarList}
        ]
        return queryset