从 Django 中的外键预填充 slug 字段

发布于 2024-11-02 10:52:21 字数 726 浏览 1 评论 0原文

我将如何从外键中预填充 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘世孤行 2024-11-09 10:52:21

如果您检查文档(http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields),它确实指出您无法引用外键字段。

看看你的设计,这不是更好吗:

class BaseModel(models.Model):
    title = models.CharField(max_length=256)
    slug = models.SlugField()

class Issue(BaseModel):
    number = models.IntegerField(help_text="Do not include the '#'.")

class ComicBookSeries(BaseModel):
    issues = models.ForeignKey(Issue)

你需要按顺序声明类!

希望有帮助!

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:

class BaseModel(models.Model):
    title = models.CharField(max_length=256)
    slug = models.SlugField()

class Issue(BaseModel):
    number = models.IntegerField(help_text="Do not include the '#'.")

class ComicBookSeries(BaseModel):
    issues = models.ForeignKey(Issue)

You need to declare the classes in that order!

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文