Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用AutoSlugField和Django标记时出现ValueError_Python_Django_Tagging - Fatal编程技术网

Python 使用AutoSlugField和Django标记时出现ValueError

Python 使用AutoSlugField和Django标记时出现ValueError,python,django,tagging,Python,Django,Tagging,我遇到ValueError,下面是,结束是: 异常类型:ValueError位于/admin/blog/post/add/ 异常值:基数为10的int()的文本无效:“treef” 我正在使用来自的AutoSlugField 我正在努力工作,这是我的模特。py: class Post(models.Model): """Blog post model.""" title = models.CharField('title', max_length=120) slug = AutoSl

我遇到ValueError,下面是,结束是:

异常类型:ValueError位于/admin/blog/post/add/
异常值:基数为10的int()的文本无效:“treef”

我正在使用来自的
AutoSlugField

我正在努力工作,这是我的模特。py:

class Post(models.Model):
  """Blog post model."""
  title = models.CharField('title', max_length=120)
  slug = AutoSlugField('slug', unique=True, max_length=120, primary_key=True)
  body = models.TextField('body')
  published = models.DateTimeField('publish', default=datetime.now)
  category = models.ForeignKey(Category)
  tags = TagField()

  class Meta:
    verbose_name = ('post')
    verbose_name_plural = ('posts')
    ordering  = ('-published',)
    get_latest_by = 'published'

  def __unicode__(self):
    return self.title

我认为这不会导致您的错误,但您在
AutoSlugField
声明中缺少
populate\u,我认为您不需要
unique=True

class Post(models.Model):
"""Blog post model."""
title = models.CharField('title', max_length=120)
slug = AutoSlugField('slug', unique=True, max_length=120, primary_key=True)
    ... snip...
我想你想要:

slug = AutoSlugField(populate_from = 'title', max_length=120, primary_key=True)
或者至少,我在中使用的是
AutoSlugField

我认为您得到了错误,因为django标记期望主键是整数,而您的主键是字符串。您的
primary\u key=True
声明是否有充分的理由?为什么不使用自动添加的
id
列的默认值呢

如果您想使用URL中的slug(这毕竟是slug的要点!)访问您的帖子,那么在
URLs.py
中给出以下条目:

url(r'post/(?P<slug>.+)/$', single_post)
或者你可以使用


您的主键不是int。

是的,这是我访问单个帖子的URL模式:URL(r'post/(?p.+)/$),single_post),因此如果slug是主键,我想知道是否没有办法解决这个问题?谢谢您编辑我的问题。我会记得进一步遵循这个更容易阅读的格式:)@user346816-我已经编辑了我的答案来解释你的评论。这有帮助吗?我使用了另一个名为django taggit的第三方标记模块,我在IRC上询问,他们指出在这个文件类中,TaggedItem(TaggedItemBase):object_id=models.IntegerField()就是罪魁祸首。我有办法解决这个问题吗?也就是说,我可以在我的Post模型中放入tags=TaggableManager(),并重写主键作为SlugField?@user346816-你真的不需要将
slug
作为主键-它真的什么都得不到。上面已经有一个
唯一的
索引。只需使用
id
列作为主键(默认行为),就完成了。我认为,如果不改变标签应用程序的内部结构,您就无法完成当前正在做的事情,这似乎是一个坏主意,几乎没有什么好处。
def single_post(request, slug):
   post = get_object_or_404(Post, slug = slug)
   ...
slug = AutoSlugField('slug', unique=True, max_length=120, primary_key=True)