Model Creation Freezing Django (or Django Rest Framework)

I have this model:

class AllowedUser(models.Model):
    PLACE_CHOICES = (
        (1, 'Loc1'),
        (2, 'Loc2'),
        (3, 'Loc3'),
        (4, 'Loc4'),
        (5, 'Loc5'),
    )
    
    id = models.CharField(primary_key=True, max_length=8, unique=True, default=generate_unique_id)
    name = models.CharField(max_length=60)
    place = models.IntegerField(choices=PLACE_CHOICES)
    current_version = models.CharField(max_length=8, default="0.0.1")
    last_updated = models.DateTimeField(default=datetime.datetime(1970,1,1,0,0,0))

    def __str__(self):
        return self.id + ' - ' + self.name

And when I try it in shell, or even in runtime with DRF, it just does nothing.

In DRF, it stops with no error right after serializer.save(), in Shell it freezes (not actually freeze, but waits for something) right after AllowedUser(...Data...). I'm using SQLite3 as database.

I actually don't know what's the root problem here. Anyone got an idea on what's causing it to hold?

Thanks in advance.

The problem started on an earlier snippet that I didn't post, this function:

def generate_unique_id():
    timeout = 0;
    while True:
        timeout += 1;
        # Generate a random 8-character alphanumeric string
        characters = string.ascii_letters + string.digits  # Include both letters and digits
        new_id = ''.join(random.choice(characters) for _ in range(8)).upper()
        if len(AllowedUser.objects.filter(id=new_id)) == 0:
            return new_id
        else:
            continue

The corrected version shown above uses AllowedUser.objects.filter, while the error-causing one was using AllowedUser.objects.create and missing a not null parameter.

Back to Top