Django AutoSlugField跨越继承的模型

时间:2013-01-22 13:04:46

标签: django django-models django-forms slug django-1.4

我想知道你是否可以在继承的模型中使用独特的slug?

我有三个模型,一个基础和两个扩展它。

基数具有从AutoSlugField字段生成的唯一name

slug位于BaseModel但是如果我保存HamsterElderberry这两个名称相同,那么我会得到IntegrityError

显然,这是因为唯一性不是在Base级别强制执行,而是在子类级别强制执行。有人有这个很好的解决方案吗?

BaseModel不是abstract,需要保持这种状态,因为我正在使用'django-model-utils' InheritanceManager。

设置

Django==1.4.3
django-extensions==1.0.2
django-autoslug==1.6.1

models.py

class BaseModel(models.Model):
    name = models.CharField(max_length=255)
    slug = AutoSlugField(populate_from='name', unique=True, db_index=True)
    # ... other fields

    objects = InheritanceManager()

class HamsterModel(BaseModel):
    useful_info = models.CharField(max_length=128)
    # ... other fields

class ElderberryModel(BaseModel):
    pointless_info = models.CharField(max_length=128)
    # ... other fields

控制台

>>> h = HamsterModel()
>>> h.name = 'One'
>>> h.save()
>>> h.slug
u'one'
>>> e = ElderberryModel()
>>> e.name = 'One'
>>> e.save()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/blah/.envs/vfaces/local/lib/python2.7/site-packages/django/db/models/base.py", line 463, in save

    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/home/blah/.envs/blah/local/lib/python2.7/site-packages/django/db/models/base.py", line 506, in save
_base
    self.save_base(cls=parent, origin=org, using=using)
  File "/home/blah/.envs/blah/local/lib/python2.7/site-packages/django/db/models/base.py", line 551, in save
_base
    result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
  File "/home/blah/.envs/blah/local/lib/python2.7/site-packages/django/db/models/manager.py", line 203, in _
insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "/home/blah/.envs/blah/local/lib/python2.7/site-packages/django/db/models/query.py", line 1593, in in
sert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/blah/.envs/blah/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 912,
 in execute_sql
    cursor.execute(sql, params)
  File "/home/blah/.envs/blah/local/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in exe
cute
    return self.cursor.execute(sql, params)
  File "/home/blah/.envs/blah/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 114,
 in execute
    return self.cursor.execute(query, args)
  File "/home/blah/.envs/vfaces/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 201, in execute
    self.errorhandler(self, exc, value)
  File "/home/blah/.envs/blah/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in default
errorhandler
    raise errorclass, errorvalue
IntegrityError: (1062, "Duplicate entry 'one' for key 'slug'")

2 个答案:

答案 0 :(得分:1)

好的,所以在挖掘完源代码之后,事实证明docs已经更新了,因为我看了之后。

因此,如果您将objects = models.Manager()添加到BaseModel并将其传递给AutoSlugField。这将检查slug对BaseModel而不是子类。

class BaseModel(models.Model):
    objects = models.Manager()
    name = models.CharField(max_length=255)
    slug = AutoSlugField(populate_from='name', unique=True, db_index=True, managers=objects)
    # ... other fields

答案 1 :(得分:0)

在数据库中,BaseModel表中只有一个子句字段,HamsterModelElderberryModel使用外键连接到BaseModel。我在这种情况下看到的唯一方法是删除unique=True并在保存方法中检查模型级别的唯一性。