在模型上运行objects.all()时的TypeError

时间:2018-01-09 13:17:08

标签: python django

这是我的模特:

import androidlab.exercise4_1.MainActivity.ListViewAdapter.ToDoItemViewHolder;
import androidlab.exercise4_1.model.ToDoItem;

我正试图从django shell中做到这一点:

class Workout(models.Model):

    datetime = models.DateTimeField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    lifts = fields.LiftsField(null=True)
    cardios = fields.CardiosField(null=True)

    def __str__(self):
        return str(self.datetime)+" "+self.user.email

    __repr__ = __str__

这个错误背后的解释是什么?我没有在任何地方发出任何论据,所以据我所知,错误必须在于我对模型的声明,但我看不出我犯的错误。感谢您的帮助。

编辑:这是 fields.py ,其中包含我的自定义字段:

(workout) Sahands-MBP:workout sahandzarrinkoub$ shell
Python 3.6.2 (default, Jul 17 2017, 16:44:45) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from workoutcal.models import Workout
>>> Workout.objects.all()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/models/query.py", line 226, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __iter__
    self._fetch_all()
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/models/query.py", line 1118, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/models/query.py", line 62, in __iter__
    for row in compiler.results_iter(results):
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 842, in results_iter
    row = self.apply_converters(row, converters)
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 827, in apply_converters
    value = converter(value, expression, self.connection, self.query.context)
TypeError: from_db_value() takes 4 positional arguments but 5 were given

1 个答案:

答案 0 :(得分:6)

您正在使用Django 2.0的文档,但似乎正在运行Django 1.11或更早版本。在该版本中,from_db_value需要额外的参数context - 请参阅the docs

此参数未使用,已在更高版本中删除。