How to set a callable function to AutoField default attribute?

Umbardacil

I'm trying to overwrite the pk of my models to use a random generator.

Here's the model field:

pk = models.AutoField(auto_created=True, primary_key=True, verbose_name='ID', default=genkey)

and the keygenerator:

def genkey():
    return random.randrange(1,142857)

So, I would like to make the autofield to execute the genkey function as much as possible until it gets a non-used key. Is there an attribute that I didn't find or do I need to code it in the generator (by getting all the used keys) ?

My main goal is to make a generator as generic as possible, not define a custom generator for each model.

Umbardacil

After making some research and tests, I ended up creating a class.

class RandomIDKey(models.Model):
    id = models.IntegerField(
        auto_created=True,
        primary_key=True,
        serialize=False,
        verbose_name='ID',
        unique=True,
        db_index=True,
        editable=False
    )

    def save(self, *args, **kwargs):
        if not self.id:
            self.id = genkey(type(self))
        super().save(*args, **kwargs)

    class Meta:
        abstract = True

with a public function:

def genkey(model):
    generated_key = random.randrange(1, 2147483648)
    if model.objects.filter(pk=generated_key).exists():
        generated_key = genkey(model)
    return generated_key

Using an AutoField is a bad idea since the field is not auto-generated by the database, hence the IntegerField

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to set a Django model field's default value to a function call / callable (e.g., a date relative to the time of model object creation)

Django -- new field: How to set default callable for existing objects

How to set up a callable for Django Imagefield's >> default << parameter

How to type hint function with a callable argument and default value

How to set the default value of directive attribute?

How to set the default value of an attribute on a Laravel model

How to set a default attribute to all elements

How to set a default model attribute if the one is missing?

How to set default value in function

how to set the callable function parameters of 'before_handler' in cherrypy

How to to set the default value of a virtual attribute using the attribute API

How to set a function attribute to a function pointer?

How to make callable attribute in Ruby

Default callable in function definition in php 7

firebase callable function appends -default during deployment

Django AutoField default value error

How to copy a function and set a default parameter in Python?

How to set function default parameter inside class?

how to cover the default value set inside the function

How to set model field default the same as OneToOneField attribute value?

How to set the value of a default attribute of a Mongoose schema based on a condition

How to set a default value for attribute if it doesn't exist in the XDocument object

Yii2: How to set default attribute values in ActiveRecord?

How to use the default value for an attribute set in nixos, and extend it

How to set default value of a new attribute for earlier items in dynamodb table?

How do I set a default FileHandle attribute with moose

How do you set default attribute values for a custom pivot table?

How to call the callable function in PHP?

Set both default attribute and type of attribute