我的模型中有以下表格。py
class ScanTarget(models.Model):
scan_target = models.CharField(primary_key=True, max_length=60)
scan_count = models.IntegerField(default=1)
last_scan = models.DateTimeField(default=timezone.now, blank=True, null=True)
created = models.DateTimeField(default=timezone.now)
def __str__(self):
return str(self.scan_target)
class RawScanResults(models.Model):
scan_target = models.ForeignKey(ScanTarget, on_delete=models.CASCADE,
related_name='raw_results')
result_uid = models.UUIDField()
start = models.DateTimeField(default=timezone.now, db_index=True)
finish = models.DateTimeField(null= True, blank=True, db_index=True)
score = models.TextField(max_length=5, blank=True, null=True)
def __str__(self):
return str('test')
当我想在我的 views.py 中使用对ScanTarget的外键实例化新的RawScanResults时出现问题,如下所示:
if not ScanTarget.objects.filter(scan_target=domain).exists():
obj=ScanTarget(scan_target=domain)
obj.save()
uid = str(uuid4())
RawScanResults.objects.create(scan_target=obj,
result_uid = uid,
start = timezone.now(),
)
我一直收到此错误:TypeError: __str__ returned non-string (type ScanTarget)
我尝试为两个类修改def __str__
或将它们注释掉,但是它一直给我同样的错误。我这是怎么了?
更新:
这是我在manage.py shell
中所遵循的:
In [119]: a=ScanTarget(scan_target='yahoo.com')
In [120]: uid
Out[120]: '1a5b2732-69f0-457a-8ec0-c6232662577e'
In [121]: a.save()
In [122]: b = RawScanResults(scan_target=a, result_uid=uid)
In [123]: b
Out[123]: ---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~/.virtualenvs/djangodev/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)
700 type_pprinters=self.type_printers,
701 deferred_pprinters=self.deferred_printers)
--> 702 printer.pretty(obj)
703 printer.flush()
704 return stream.getvalue()
~/.virtualenvs/djangodev/lib/python3.5/site-packages/IPython/lib/pretty.py in pretty(self, obj)
393 if callable(meth):
394 return meth(obj, self, cycle)
--> 395 return _default_pprint(obj, self, cycle)
396 finally:
397 self.end_group()
~/.virtualenvs/djangodev/lib/python3.5/site-packages/IPython/lib/pretty.py in _default_pprint(obj, p, cycle)
508 if _safe_getattr(klass, '__repr__', None) is not object.__repr__:
509 # A user-provided repr. Find newlines and replace them with p.break_()
--> 510 _repr_pprint(obj, p, cycle)
511 return
512 p.begin_group(1, '<')
~/.virtualenvs/djangodev/lib/python3.5/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
699 """A pprint that just redirects to the normal repr function."""
700 # Find newlines and replace them with p.break_()
--> 701 output = repr(obj)
702 for idx,output_line in enumerate(output.splitlines()):
703 if idx:
~/.virtualenvs/djangodev/lib/python3.5/site-packages/django/db/models/base.py in __repr__(self)
511
512 def __repr__(self):
--> 513 return '<%s: %s>' % (self.__class__.__name__, self)
514
515 def __str__(self):
TypeError: __str__ returned non-string (type ScanTarget)
答案 0 :(得分:0)
我删除了所有数据库表,并发布了MyJSON.Data.Houses
和python manage.py makemigrations
,问题得到解决。
我不知道是什么原因造成的。