How To Implement Multivalued Attribute In Django?
Solution 1:
In Django, all the Models (written for you or generated with inspectdb) must kave Primary Key.
If you like to use the table phonenumbers like a Model, you will need that this table have a primary key.
In this scenario, my advice is that you modify your legacy table to add a new primary key manually and discard your composite primary key. Your legacy table have a composite primary key (phone_number+area_code) and this is not supported officially in Django.
Solution 2:
Django does not support composite primary keys and your PhoneNumbers table has a primary key that spans three columns. This ticket has been open for years. There is a third party plugin that provide composite primary key support but it's been unmaintained for two years and incompatible with the latest versions of Django.
The solution is to add the primary key. But before that do
./manage.py migrate
This will make sure that the tables needed by django are created in your legacy database.
Now modify your models to delete this line
managed = False
This signals django that alterations to the models are to be reflected in the database. Then change your model as follows.
class Person(models.Model):
id = models.BigIntegerField(primary_key=True, db_column='person_id')
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
class Meta:
db_table = 'person'
class PhoneNumbers(models.Model):
id = models.BigIntegerField(primary_key=True)
person = models.ForeignKey(Person, models.DO_NOTHING)
phone_number = models.CharField(max_length=15)
area_code = models.CharField(max_length=15)
class Meta:
db_table = 'phonenumbers'
unique_together = (('person', 'phone_number', 'area_code'),)
Then do
./manage.py makemigrations your_app_name
./manage.py migrate
Notice that I have renamed the primary key field in Person. This is just a cosmetic change. That's because the convention is to have the primary key field as id. When you are dealing with lots of models you are likely to forget that this model's primary key was named differently. Hence the change.
Post a Comment for "How To Implement Multivalued Attribute In Django?"