英文:
AttributeError: 'Project' object has no attribute 'remark_set'
问题
以下是您要翻译的内容:
Error:
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lavis\Desktop\aai\env\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lavis\Desktop\aai\base\views.py", line 80, in project
project = project.remark_set.all()
^^^^^^^^^^^^^^^^^^
AttributeError: 'Project' object has no attribute 'remark_set'
models.py:
class Project(models.Model):
host = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
name = models.CharField(max_length=200)
location = models.ForeignKey(Location, on_delete=models.SET_NULL, null=True)
totalamount = models.IntegerField(null=True, blank=True)
payments_todate = models.IntegerField(null=True, blank=True)
description = models.TextField(null=True, blank=True)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-updated', '-created']
def __str__(self):
return self.name
class Remark(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
project = models.ForeignKey(Project, related_name='project_content_type', on_delete=models.CASCADE)
body = models.TextField()
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.body[0:50]
views.py:
def project(request, pk):
project = Project.objects.get(id=pk)
remark_made = project.remark_set.all()
context = {'project': project, 'remark_made': remark_made}
return render(request, 'base/project.html', context)
英文:
I am new to django, was following a tutorial and trying to use 'model_set.all()' but I don't understand the error. I tried changing it to 'project_content_type.remark_set.all()' (thinking it's an error with the related name.
Error:
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lavis\Desktop\aai\env\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lavis\Desktop\aai\base\views.py", line 80, in project
project = project.remark_set.all()
^^^^^^^^^^^^^^^^^^
AttributeError: 'Project' object has no attribute 'remark_set'
models.py
class Project(models.Model):
host = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
#p_id =
name = models.CharField(max_length=200)
location = models.ForeignKey(Location, on_delete=models.SET_NULL, null=True)
totalamount = models.IntegerField(null=True, blank=True)
payments_todate = models.IntegerField(null=True,blank=True)
description = models.TextField(null=True,blank = True)
updated = models.DateTimeField(auto_now = True)
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-updated','-created']
def __str__(self):
return self.name
class Remark(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
project = models.ForeignKey(Project,related_name='project_content_type', on_delete=models.CASCADE)
body = models.TextField()
updated = models.DateTimeField(auto_now = True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.body[0:50]
views.py
def project(request, pk):
project = Project.objects.get(id=pk)
remark_made = project.remark_set.all()
context = {'project':project, 'remark_made':remark_made}
return render(request, 'base/project.html',context)
答案1
得分: 2
你在模型Remark中定义了related_name='project_content_type'
,因此你需要更改以下部分:
remark_made = project.project_content_type.all()
related_name
定义了相关管理器的名称。如果将其删除,将使用默认的Model + "_set"
(在你的情况下是remark_set
)。
英文:
you define related_name='project_content_type'
in model Remark, so you need to change
remark_made = project.project_content_type.all()
related_name
defines the name of the related manager. If you remove it, default Model + "_set"
is used (remark_set
in your case).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论