我想组装一篇文章的 URL,并在保存实例后立即将其发送到某处。
一切都很好,除了我无法获得 URL 的一部分,即“类别”,它应该是 ManyToMany 对象的第一项。
我想得到这样的结果: http ://www.example.com/category/article.html
编码:
class Categories(models.Model):
...
name = models.CharField(max_length=150, blank=False)
slug = models.SlugField()
class Texts(models.Model):
...
slug = models.SlugField()
title = models.CharField(max_length=150, blank=False)
subtitle = models.TextField(blank=True)
cetegory = models.ManyToManyField(to=Categories, blank=True)
def get_absolute_url(self):
#firstpart = self.category.all() #returns empty list
#firstpart = self.category #returns empty list
#firstpart = Categories.objects.all().filter(texts__slug = self.slug) #returns empty list
lastpart = self.Slug #this is ok
return firstpart[0] + "/" + lastpart + ".html"
models.signals.post_save.connect(post_to_twitter, sender=Texts)
...是的,我知道 reverse(),我会使用它,但首先我想知道参数
谢谢