在Django Model中获取完全限定的URL

时间:2015-03-20 00:17:49

标签: django django-models

如何在Web请求上下文之外(即在Model属性getter中)生成完全限定的URL(包括协议和端口)?

例如,我有一个模型如下:

class Package(models.Model):
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=500)
    version = models.CharField(max_length=30)
    pub_date = models.DateTimeField('date published')
    filename = models.CharField(max_length=200)
    is_published = models.BooleanField()
    release_notes_url = models.CharField(max_length=1000)
    platform = models.ForeignKey(Platform)
    groups = models.ManyToManyField(Group)

    @property
    def package_url(self):
        return <<some root url>> + "/foo/" + self.filename

package_url我想获得完整的合格网址。

0 个答案:

没有答案