Django中的自定义链接

发布于 2025-02-13 02:53:24 字数 598 浏览 1 评论 0 原文

这是我的初始代码,

class Article(models.Model):
    article_author  = models.CharField(max_length=12)
    article_name    = models.CharField(max_length=60)
    article_body    = models.TextField()
    article_date    = models.DateTimeField(auto_now_add=True)
    article_tags    = models.CharField(max_length=25, choices=tags_, null=True)
    article_link    = make_link()  

事实证明它不会添加到数据库中,也无法进行迁移(因为它不是.models的一部分)。  (我希望将链接自动作为创建文章类的实例,没有用户的实例),

我可以将其制成charfield,然后用该功能替换那里的任何内容,但这似乎是一个草率的解决方案,也是如此'将为默认管理面板提供一个无用的字段。

Here's my initial code

class Article(models.Model):
    article_author  = models.CharField(max_length=12)
    article_name    = models.CharField(max_length=60)
    article_body    = models.TextField()
    article_date    = models.DateTimeField(auto_now_add=True)
    article_tags    = models.CharField(max_length=25, choices=tags_, null=True)
    article_link    = make_link()  

Turns out it won't be added to the db, and I can't make migrations (prob. because it's not part of .models).
(I want the link to be made automatically as the instance of article class is created, and without user)

I can just make it a CharField and then replace whatever was there with the function, but that just seems like a sloppy solution, also it'll give a usless field to the default admin-panel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

少年亿悲伤 2025-02-20 02:53:24

您可以将链接属性添加到您的模型,因为

class Article(models.Model):
    ...
    # article_link    = make_link()  

    @property
    def link(self):
        return make_link()

您可以像 acrate.link 一样轻松地访问它。

you can add a link property to your model as

class Article(models.Model):
    ...
    # article_link    = make_link()  

    @property
    def link(self):
        return make_link()

then you can access it easily just like article.link

情深如许 2025-02-20 02:53:24

我建议在 save()上生成链接,然后将其存储在 charfield 中,用 editable = false = false ,就像Willem所建议的那样。

文档: https://djangoproject.com/4.0 /ref/models/fields/#可编辑

class Article(models.Model):
    article_author  = models.CharField(max_length=12)
    article_name    = models.CharField(max_length=60)
    article_body    = models.TextField()
    article_date    = models.DateTimeField(auto_now_add=True)
    article_tags    = models.CharField(max_length=25, choices=tags_, null=True)
    article_link    = models.CharField(max_length=200, editable=False)

    def save(self):
        self.article_link = make_link()
        super().save()

或,如果文章链接遵循某些逻辑,则可能无需生成并存储链接,但可以作为属性即时访问它。

Django具有 get_absolute_url()方法,而不是属性,用于为模型对象生成URL:

class Article(models.Model):
    article_author  = models.CharField(max_length=12)
    article_name    = models.CharField(max_length=60)
    article_body    = models.TextField()
    article_date    = models.DateTimeField(auto_now_add=True)
    article_tags    = models.CharField(max_length=25, choices=tags_, null=True)

    @property
    def article_link(self):
        return make_link()

I would suggest generating the link on save() and store it in a CharField with editable=False, just like Willem suggested.

Docs: https://docs.djangoproject.com/en/4.0/ref/models/fields/#editable

class Article(models.Model):
    article_author  = models.CharField(max_length=12)
    article_name    = models.CharField(max_length=60)
    article_body    = models.TextField()
    article_date    = models.DateTimeField(auto_now_add=True)
    article_tags    = models.CharField(max_length=25, choices=tags_, null=True)
    article_link    = models.CharField(max_length=200, editable=False)

    def save(self):
        self.article_link = make_link()
        super().save()

Or, if the article link is following some logic, there might be no need to generate and store the link, but access it on the fly as a property.

Django has the get_absolute_url() method, not a property, for generating URLs for an model object:
https://docs.djangoproject.com/en/4.0/ref/models/instances/#get-absolute-url

class Article(models.Model):
    article_author  = models.CharField(max_length=12)
    article_name    = models.CharField(max_length=60)
    article_body    = models.TextField()
    article_date    = models.DateTimeField(auto_now_add=True)
    article_tags    = models.CharField(max_length=25, choices=tags_, null=True)

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