Reputation: 2568
I have a legacy database not created by django with the following table:
$ describe `time`;
+---------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+-------------------+-----------------------------+
| data | varchar(16) | NO | MUL | NULL | |
| source | varchar(255) | NO | | NULL | |
| source_origin | varchar(128) | YES | | NULL | |
| sys_updated | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+---------------+--------------+------+-----+-------------------+-----------------------------+
I get the model of this table by inspectdb
:
class Time(models.Model):
data = models.ForeignKey('a_table', models.DO_NOTHING)
source = models.CharField(max_length=255)
source_origin = models.CharField(max_length=128, blank=True, null=True)
sys_updated = models.DateTimeField()
class Meta:
managed = False
db_table = 'time'
unique_together = (('data', 'source_origin'),)
A have a simple serializer:
class TimeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Time
fields = ('data', 'source', 'source_origin', 'sys_updated')
And a simple view
:
class TimeList(generics.ListAPIView):
""" Retrieves all data. """
queryset = Time.objects.all()
serializer_class = TimeSerializer
When I try to access this from, let's say, localhost:8765/time/
, I'm getting the following error:
OperationalError: (1054, "Unknown column 'time.id' in 'field list'")
My question: Is it posible to have a table to the django rest without an id? I don't want to add an id at my table.
I found some answers in the wild, but it did not help.
Upvotes: 3
Views: 1813
Reputation: 2746
From the doc:
Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).
https://docs.djangoproject.com/en/1.9/topics/db/models/#automatic-primary-key-fields
So I'm afraid the answer is no. You can try and set the primary_key=True on one of the other fields if that works for you..
Upvotes: 5