Hello again,

I just thought I'd try to make it clearer what I'm after. Instead of
having numbers as ID for my objects, I'd like to have random strings
(e.g. "sadfwetbtyvt32452" or "fd70982876adhfd"...). Those strings have
to be unique across the table.
Is that easily achievable? Am I on the right track with the code
above?

Thanks!

Julien

On Mar 31, 5:16 pm, Julien <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'd like to have a random string as id for my class Project instead of
> the default auto-incremental number. I suppose I need to start like
> this:
>
> class Project(models.Model):
>     id = RandomStringID(primary_key=True)
>     name = models.CharField(_('Name'), max_length=200)
>     description = models.TextField(blank = True)
>
> Then, based on the definition of AutoField, I did:
>
> class RandomStringIDField(Field):
>     empty_strings_allowed = False
>     def __init__(self, *args, **kwargs):
>         assert kwargs.get('primary_key', False) is True, "%ss must
> have primary_key=True." % self.__class__.__name__
>         kwargs['blank'] = True
>         Field.__init__(self, *args, **kwargs)
>
>     def to_python(self, value):
>         return value
>
>     def get_manipulator_fields(self, opts, manipulator, change,
> name_prefix='', rel=False, follow=True):
>         if not rel:
>             return [] # Don't add a FormField unless it's in a related
> context.
>         return Field.get_manipulator_fields(self, opts, manipulator,
> change, name_prefix, rel, follow)
>
>     def get_manipulator_field_objs(self):
>         return [oldforms.HiddenField]
>
>     def get_manipulator_new_data(self, new_data, rel=False):
>         # Never going to be called
>         # Not in main change pages
>         # ignored in related context
>         if not rel:
>             return None
>         return Field.get_manipulator_new_data(self, new_data, rel)
>
>     def contribute_to_class(self, cls, name):
>         assert not cls._meta.has_random_string_id_field, "A model
> can't have more than one RandomStringIDField."
>         super(AutoField, self).contribute_to_class(cls, name)
>         cls._meta.has_random_string_id_field = True
>         cls._meta.random_string_id_field = self
>
>     def formfield(self, **kwargs):
>         return None
>
> Now, I don't really know where to go from there. What do I need to do
> so it works transparently as a primary key? Also, where should the
> random string generation happen?
>
> Thanks a lot!
>
> Julien
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to