从 Django 中的外键预填充 slug 字段
我将如何从外键中预填充 slug?以下是我的一些模型的设置方式:
Class Title(models.Model):
title = models.CharField(max_length=256)
slug = models.SlugField()
class Issue(models.Model):
title = models.ForeignKey(Title)
number = models.IntegerField(help_text="Do not include the '#'.")
slug = models.SlugField()
admin.py:
class IssueAdmin (admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
admin.site.register(Issue, IssueAdmin)
问题预填充的是外键的 ID,但我想我需要它来预填充外键的 slug。我该怎么做呢?我正在使用 Django 1.3。我检查了其他线程,但它们似乎引用了几年前的 Django 版本,但不再工作了。
我需要标题来显示问题列表。到目前为止,它有效。您可以单击问题的链接来查看问题显示的内容。
我觉得好像将标题重新设计为抽象类,就像 Skidoosh 不允许我查看对象子集一样......
How would I go about prepopulating a slug from a foreign key? Here are how some of my models are setup:
Class Title(models.Model):
title = models.CharField(max_length=256)
slug = models.SlugField()
class Issue(models.Model):
title = models.ForeignKey(Title)
number = models.IntegerField(help_text="Do not include the '#'.")
slug = models.SlugField()
admin.py:
class IssueAdmin (admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
admin.site.register(Issue, IssueAdmin)
What the Issue prepopulates is the ID of the foreign key, but I suppose I would need it to preopulate the slug of the foreign key. How would I go about doing this? I am using Django 1.3. I have checked other threads, but they seem to refer to version of Django a few years older that don't work anymore.
I need the Titles to display the list of issues. So far, it works. And you can click on the link to the issue to see what the issue displays.
I feel as if reworking the Title to abstract classes the way Skidoosh will not allow me to view subsets of objects....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您检查文档(http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields),它确实指出您无法引用外键字段。
看看你的设计,这不是更好吗:
你需要按顺序声明类!
希望有帮助!
If you check the docs (http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields) it does state that you can't reference a foreign key field.
Looking at you design would this not work better:
You need to declare the classes in that order!
Hope that helps!