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'),
]

1 Answers1

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.

Sabil
  • 3,750
  • 1
  • 5
  • 16
  • So when I run the second "migrate" I get this error: django.db.utils.ProgrammingError: column "id" of relation "books_book" does not exist – ProgrammingProbie12 Sep 16 '21 at 17:06
  • delete all previous migrations and start from scratch. hope it will resolve your issue – Sabil Sep 16 '21 at 17:08
  • 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! – ProgrammingProbie12 Sep 16 '21 at 19:01
  • If my answer solves your issue then mark this as an accepted answer and give an upvote – Sabil Sep 16 '21 at 19:11