使用__str__方法获取django对象
我有这个称为子句
的Django模型,并且我有一个为其定义的__ str __
方法。 现在,我的表单显示了该子句的下拉列表,但是当我发布该表单时,它将这些子句转换为字符串,而我需要rales
objects。如果我有.__ str __()
,是否有办法获得子句? 模型:
class Clause(models.Model):
reference_number = models.CharField(max_length=1000)
title = models.CharField(max_length=1000)
def __str__(self):
return self.reference_number + ": " + self.title
I have this Django model called Clause
and I have a __str__
method defined for it.
Now I have a form that displays a dropdown for the clauses, but when I POST that form, it converts those clauses to strings, whereas I need the Clause
objects. Is there a way to get the clause if I have it's .__str__()
?
The model:
class Clause(models.Model):
reference_number = models.CharField(max_length=1000)
title = models.CharField(max_length=1000)
def __str__(self):
return self.reference_number + ": " + self.title
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
__ str __
方法中,您可能已经使用了子句
的名称,标题或其他属性。您可以使用该属性获取特定对象。如果您的
条款
定义是如此:在您的视图函数中:
In your
__str__
method you probably have used the name, title or another attribute of theClause
. You can use that attribute to get the particular object.If your
Clause
definition is like so:In your view function: