Python django.db.models 模块,UUIDField() 实例源码

我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用django.db.models.UUIDField()

项目:django-twilio-tfa    作者:rtindru    | 项目源码 | 文件源码
def url_str_to_user_pk(s):
    User = get_user_model()
    # TODO: Ugh, isn't there a cleaner way to determine whether or not
    # the PK is a str-like field?
    if getattr(User._meta.pk, 'rel', None):
        pk_field = User._meta.pk.rel.to._meta.pk
    else:
        pk_field = User._meta.pk
    if (hasattr(models, 'UUIDField') and issubclass(
            type(pk_field), models.UUIDField)):
        return s
    try:
        pk_field.to_python('a')
        pk = s
    except ValidationError:
        pk = base36_to_int(s)
    return pk
项目:Provo-Housing-Database    作者:marcopete5    | 项目源码 | 文件源码
def url_str_to_user_pk(s):
    User = get_user_model()
    # TODO: Ugh, isn't there a cleaner way to determine whether or not
    # the PK is a str-like field?
    if getattr(User._meta.pk, 'rel', None):
        pk_field = User._meta.pk.rel.to._meta.pk
    else:
        pk_field = User._meta.pk
    if (hasattr(models, 'UUIDField')
            and issubclass(type(pk_field), models.UUIDField)):
        return s
    try:
        pk_field.to_python('a')
        pk = s
    except ValidationError:
        pk = base36_to_int(s)
    return pk
项目:sdining    作者:Lurance    | 项目源码 | 文件源码
def get_pk_description(model, model_field):
    if isinstance(model_field, models.AutoField):
        value_type = _('unique integer value')
    elif isinstance(model_field, models.UUIDField):
        value_type = _('UUID string')
    else:
        value_type = _('unique value')

    return _('A {value_type} identifying this {name}.').format(
        value_type=value_type,
        name=model._meta.verbose_name,
    )
项目:djangoql    作者:ivelum    | 项目源码 | 文件源码
def get_field_cls(self, field):
        str_fields = (models.CharField, models.TextField, models.UUIDField)
        if isinstance(field, str_fields):
            return StrField
        elif isinstance(field, (models.AutoField, models.IntegerField)):
            return IntField
        elif isinstance(field, (models.BooleanField, models.NullBooleanField)):
            return BoolField
        elif isinstance(field, (models.DecimalField, models.FloatField)):
            return FloatField
        elif isinstance(field, models.DateTimeField):
            return DateTimeField
        elif isinstance(field, models.DateField):
            return DateField
        return DjangoQLField
项目:django-twilio-tfa    作者:rtindru    | 项目源码 | 文件源码
def setUp(self):
        if hasattr(models, 'UUIDField'):
            self.user_id = uuid.uuid4().hex

            class UUIDUser(AbstractUser):
                id = models.UUIDField(primary_key=True,
                                      default=uuid.uuid4,
                                      editable=False)

                class Meta(AbstractUser.Meta):
                    swappable = 'AUTH_USER_MODEL'
        else:
            UUIDUser = get_user_model()
        self.UUIDUser = UUIDUser
项目:django-twilio-tfa    作者:rtindru    | 项目源码 | 文件源码
def user_pk_to_url_str(user):
    """
    This should return a string.
    """
    User = get_user_model()
    if (hasattr(models, 'UUIDField') and issubclass(
            type(User._meta.pk), models.UUIDField)):
        if isinstance(user.pk, six.string_types):
            return user.pk
        return user.pk.hex

    ret = user.pk
    if isinstance(ret, six.integer_types):
        ret = int_to_base36(user.pk)
    return str(ret)
项目:graphene-django    作者:graphql-python    | 项目源码 | 文件源码
def test_should_auto_convert_id():
    assert_conversion(models.UUIDField, graphene.UUID)
项目:gougo    作者:amaozhao    | 项目源码 | 文件源码
def test_primary_key_custom_field_problem(self):
        """
        object retrieval problem occuring with some custom primary key fields (UUIDField as test case)
        """
        UUIDProject.objects.create(topic="John's gathering")
        UUIDArtProject.objects.create(topic="Sculpting with Tim", artist="T. Turner")
        UUIDResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")

        qs = UUIDProject.objects.all()
        ol = list(qs)
        a = qs[0]
        b = qs[1]
        c = qs[2]
        self.assertEqual(len(qs), 3)
        self.assertIsInstance(a.uuid_primary_key, uuid.UUID)
        self.assertIsInstance(a.pk, uuid.UUID)

        res = re.sub(' "(.*?)..", topic', ', topic', repr(qs))
        res_exp = """[ <UUIDProject: uuid_primary_key (UUIDField/pk), topic (CharField) "John's gathering">,
  <UUIDArtProject: uuid_primary_key (UUIDField/pk), topic (CharField) "Sculpting with Tim", artist (CharField) "T. Turner">,
  <UUIDResearchProject: uuid_primary_key (UUIDField/pk), topic (CharField) "Swallow Aerodynamics", supervisor (CharField) "Dr. Winter"> ]"""
        self.assertEqual(res, res_exp)

        a = UUIDPlainA.objects.create(field1='A1')
        b = UUIDPlainB.objects.create(field1='B1', field2='B2')
        c = UUIDPlainC.objects.create(field1='C1', field2='C2', field3='C3')
        qs = UUIDPlainA.objects.all()
        # Test that primary key values are valid UUIDs
        self.assertEqual(uuid.UUID("urn:uuid:%s" % a.pk, version=1), a.pk)
        self.assertEqual(uuid.UUID("urn:uuid:%s" % c.pk, version=1), c.pk)
项目:omni-forms    作者:omni-digital    | 项目源码 | 文件源码
def test_initial(self):
        """
        The model should have an initial field
        """
        field = OmniUUIDField._meta.get_field('initial')
        self.assertIsInstance(field, models.UUIDField)
        self.assertTrue(field.blank)
        self.assertTrue(field.null)
项目:omni-forms    作者:omni-digital    | 项目源码 | 文件源码
def test_field_class(self):
        """
        The model should define the correct field class
        """
        self.assertEqual(OmniUUIDField.FIELD_CLASS, 'django.forms.UUIDField')
项目:Provo-Housing-Database    作者:marcopete5    | 项目源码 | 文件源码
def setUp(self):
        if hasattr(models, 'UUIDField'):
            self.user_id = uuid.uuid4().hex

            class UUIDUser(AbstractUser):
                id = models.UUIDField(primary_key=True,
                                      default=uuid.uuid4,
                                      editable=False)

                class Meta(AbstractUser.Meta):
                    swappable = 'AUTH_USER_MODEL'
        else:
            UUIDUser = get_user_model()
        self.UUIDUser = UUIDUser
项目:Provo-Housing-Database    作者:marcopete5    | 项目源码 | 文件源码
def user_pk_to_url_str(user):
    """
    This should return a string.
    """
    User = get_user_model()
    if (hasattr(models, 'UUIDField')
            and issubclass(type(User._meta.pk), models.UUIDField)):
        if isinstance(user.pk, six.string_types):
            return user.pk
        return user.pk.hex

    ret = user.pk
    if isinstance(ret, six.integer_types):
        ret = int_to_base36(user.pk)
    return str(ret)