Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use text type as primary key in Django

Tags:

python

django

I'm learning Django but the ORM doesn't give a way to have a text primary key.I would like to have a primary such as 00RTTIN223.Is there any other way to bypass that limite?

like image 929
Christian Lisangola Avatar asked Sep 16 '25 13:09

Christian Lisangola


1 Answers

This is a snippet from django docs: https://docs.djangoproject.com/en/1.11/topics/db/models/

class Fruit(models.Model):
    name = models.CharField(max_length=100, primary_key=True)

>>> fruit = Fruit.objects.create(name='Apple')
>>> fruit.name = 'Pear'
>>> fruit.save()
>>> Fruit.objects.values_list('name', flat=True)
['Apple', 'Pear']
like image 99
e.arbitrio Avatar answered Sep 18 '25 09:09

e.arbitrio