我自己找到了答案,但认为该问题可能会使其他人受益:
每当我打电话时,我都会收到以下错误消息:
Schema.objects.all()
或
Schema.objects.filter(url=some_url)
或类似的..
错误:“'ReverseManyToOneDescriptor'对象没有属性'all'”
class Schema(models.Model):
label = models.TextField()
description = models.TextField()
url = models.URLField(unique=True)
def __str__(self):
return self.label
class Meta:
app_label = 'metadata'
class Object(models.Model):
label = models.TextField()
description = models.TextField()
# related name should not be objects, because that will cause problems
schema = models.ForeignKey(
Schema, related_name='objects', on_delete=models.CASCADE)
def __str__(self):
return "%s.%s" % (self.schema.label, self.label)
class Meta:
app_label = 'metadata'
答案 0 :(得分:1)
问题是related_name不能是“对象”,它与Schema.objects冲突
因此,通过重命名related_name例如:
class Object(models.Model):
label = models.TextField()
description = models.TextField()
# related name should not be objects, because that will cause problems
schema = models.ForeignKey(
Schema, related_name='object_list', on_delete=models.CASCADE)
def __str__(self):
return "%s.%s" % (self.schema.label, self.label)
class Meta:
app_label = 'metadata'
为我解决了这个问题。