Opened 6 years ago

Closed 6 years ago

#30006 closed Bug (duplicate)

Wrong UUID serialization in inline admin when used as a 1to1Field primary key of a UUID primary key.

Reported by: George Cheng Owned by: nobody
Component: Database layer (models, ORM) Version: 2.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Sorry for the confusing title, I create a reproducing repo on GitHub.

Key code as follows:

models.py

class Room(models.Model):
    id = models.UUIDField(default=uuid4, primary_key=True, editable=False)
    name = models.TextField()

    def __str__(self):
        return self.name


class Teacher(models.Model):
    room = models.OneToOneField(Room, on_delete=models.CASCADE,
                                primary_key=True)
    name = models.TextField()

    def __str__(self):
        return self.name


class Student(models.Model):
    id = models.UUIDField(default=uuid4, primary_key=True, editable=False)
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
    name = models.TextField()

    def __str__(self):
        return self.name

admin.py

from .models import Room, Teacher, Student


@admin.register(Room)
class RoomAdmin(admin.ModelAdmin):
    pass


@admin.register(Teacher)
class TeacherAdmin(admin.ModelAdmin):
    class StudentInline(admin.TabularInline):
        model = Student

    inlines = [StudentInline]

Change History (3)

comment:1 by George Cheng, 6 years ago

Found a workaround: add readonly_fields = ('teachers',) to student inline works.

comment:2 by George Cheng, 6 years ago

>>> from app.models import *
>>> Teacher.objects.last().room_id
UUID('38c7b7f8-1006-467c-809b-e95546dcc58e')
>>> Student.objects.last().teacher_id
'38c7b7f81006467c809be95546dcc58e'

They are all UUIDs

comment:3 by Simon Charette, 6 years ago

Component: contrib.adminDatabase layer (models, ORM)
Resolution: duplicate
Status: newclosed

Duplicate of #27595 which will be fixed in Django 2.2.

Note: See TracTickets for help on using tickets.
Back to Top