1

UPDATE: I have got it to work! What I did was:

  • Deleted the app
  • Went into Postgres Admin and deleted the Schema for that app
  • Created the app again (using a diff name)
  • Created the Models, URLs, Admin Registering, Views

So I've been trying to add a UUID field to my model, to use it in my URL but I keep getting this error

django.db.utils.ProgrammingError: column "id" is of type bigint but expression is of type uuid LINE 1: ..._book" ("id", "title", "author", "price") VALUES ('f6b15400-... ^ HINT: You will need to rewrite or cast the expression.

Here is my models.py:

from django.db import models
import uuid
from django.urls import reverse
# Create your models here.

class Book(models.Model):
    id = models.UUIDField(primary_key=True, unique=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=6, decimal_places=2)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('book_detail', args=[str(self.id)]) 

Here is the views.py:

from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Book

# Create your views here.

class BookListView(ListView):
    model = Book
    template_name = 'books/book_list.html'
    context_object_name = 'book_list'


class BookDetailView(DetailView):
    model = Book
    template_name = 'books/book_detail.html'
    context_object_name = 'book'

Here is the urls.py:

from django.urls import path
from .views import BookListView, BookDetailView

urlpatterns = [
    path('', BookListView.as_view(), name='book_list'),
    path('<uuid:pk>/', BookDetailView.as_view(), name='book_detail'),
]
3
  • Why not name the uuid field uuid instead of id? Commented Sep 16, 2021 at 16:20
  • Nope didn't work Commented Sep 16, 2021 at 16:22
  • Don't override the id field. Commented Sep 16, 2021 at 16:22

1 Answer 1

1

First add the uuid field as a normal field with a different name:

from django.db import models
import uuid
from django.urls import reverse


class Book(models.Model):
    uuid = models.UUIDField(default=uuid.uuid4, unique=True)
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=6, decimal_places=2)

Run makemigrations and migrate

Now make it primary key:

class Book(models.Model):
    uuid = models.UUIDField(default=uuid.uuid4, primary_key=True)
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=6, decimal_places=2)

This should generate a migration that removes the id field and makes uuid primary key (makemigrations and migrate again).

And finally:

class Book(models.Model):
    id = models.UUIDField(primary_key=True, unique=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=6, decimal_places=2)

You may have to adjust the generated migration.

Sign up to request clarification or add additional context in comments.

4 Comments

So when I run the second "migrate" I get this error: django.db.utils.ProgrammingError: column "id" of relation "books_book" does not exist
delete all previous migrations and start from scratch. hope it will resolve your issue
That did work! Deleted the app then went into Postgres admin and deleted the Schema, created the app again (with a different name) and its working again!
If my answer solves your issue then mark this as an accepted answer and give an upvote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.