Django 管理外键下拉字段列表仅“测试对象”
我有这两个类:
class Test(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(User)
groups = models.ManyToManyField(Group)
class TestSubjectSet(models.Model):
id = models.AutoField(primary_key=True)
test = models.ForeignKey(Test)
subject = models.ManyToManyField(Subject)
TestSubjectSet 表单测试列表仅显示字符串“测试对象”。
I have these two classes:
class Test(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(User)
groups = models.ManyToManyField(Group)
class TestSubjectSet(models.Model):
id = models.AutoField(primary_key=True)
test = models.ForeignKey(Test)
subject = models.ManyToManyField(Subject)
The TestSubjectSet form test list shows only string "test object".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须在模型类中添加
__unicode__(self)
或__str__(self)
方法。http:// docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#django.db.models.Model.unicode
You have to add
__unicode__(self)
or__str__(self)
methods in your models class.http://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#django.db.models.Model.unicode
有同样的问题。添加
解决了问题。
Had the same problem. Adding
solved the problem.
有时您希望从 str 函数返回不同的信息,但希望在管理的下拉列表中显示一些不同的信息。那么上述解决方案将不起作用。
您可以通过像这样子类化 forms.ModelChoiceField 来做到这一点。
然后您可以覆盖
formfield_for_foreignkey
Sometimes you want to have different info returned from str function but Want to show some different info in the dropdown of admin. Then Above mentioned solution won't work.
You can do this by subclassing forms.ModelChoiceField like this.
You can then override
formfield_for_foreignkey