2013-01-22 216 views
0

我想知道如果你可以在繼承模型中具有獨特的slug嗎?Django AutoSlugField跨繼承模型​​

我有三個模型,一個基礎和兩個擴展它。

該基地具有從name字段生成的唯一AutoSlugField

嵌塞是在BaseModel但是如果我救一個HamsterElderberry都具有相同的名稱,然後我得到一個IntegrityError

很明顯,這是因爲獨特性不是在基本層次而是在子類層次上實施的。有沒有人有這個好的解決方案?

BaseModel不是abstract,並且需要保持這種狀態,因爲我使用的是繼承管理器'django-model-utils'

設置

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'") 

回答

1

好的,通過源代碼挖掘後,事實證明docs已經更新,因爲我持續看了。

因此,如果您將objects = models.Manager()添加到您的BaseModel並將其傳遞給AutoSlugField。這將檢查子彈對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 
0

在數據庫中你只有一個金屬塊網絡在BaseModel表中,HamsterModelElderberryModel使用外鍵連接到BaseModel。我在這種情況下看到的唯一方法是刪除unique=True並在保存方法中檢查模型級別的唯一性。