Reputation: 1609
I have an abstract class as:
class AbstractExecutive(models.Model):
mobile = models.CharField(max_length=10,unique=True,
verbose_name='*Mobile')
#other attributs not required....
class Meta:
abstract = True
I am inheriting this class to create different instances like Client,Vendor etc. For a class instance Client, I require that the unique constraint is dropped, while it exists for other class objects. I am using postgresql 9.1 I I dropped the the client table constraint using psql, but since the model is inherited, it always has unique constraint on it. Please note the Client table has data and cannot be disturbed. How can I get rid of the constraint in the table. Client class model:
class Client(AbstractAddress,AbstractExecutive):
number = models.CharField(max_length=10,verbose_name='number',
unique=True)
#other attributes...
Upvotes: 1
Views: 1625
Reputation: 396
Maybe it wasn't possible with old Django versions, but now the solution is to re-define the attribute on the child class, without the unique=True
constraint.
So in the given example, with the unique mobile
attribute in the AbstractExecutive
abstract model and that must not be unique
in Client
model:
Your abstract class:
class AbstractExecutive(models.Model):
mobile = models.CharField(max_length=10, unique=True,
verbose_name='*Mobile')
#other attributs not required....
class Meta:
abstract = True
The child non-abstract class:
class Client(AbstractAddress, AbstractExecutive):
mobile = models.CharField(max_length=10, verbose_name='*Mobile')
#other attributes...
When generating the migrations, the unique
constraint will be removed in Client
model.
Upvotes: 0
Reputation: 23871
You could try to override the inherited mobile
field of Client
:
class Client(...):
...
Client._meta.get_field('mobile')._unique = False
Upvotes: 4
Reputation: 15104
Unfortuanately this is not possible in django (https://docs.djangoproject.com/en/dev/topics/db/models/#field-name-hiding-is-not-permitted). You need to remove mobile
from your abstract class and put it to the concrete classes (either with or without unique
).
Upvotes: 1