我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用django.db.models.UUIDField()。
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
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
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, )
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
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
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)
def test_should_auto_convert_id(): assert_conversion(models.UUIDField, graphene.UUID)
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)
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)
def test_field_class(self): """ The model should define the correct field class """ self.assertEqual(OmniUUIDField.FIELD_CLASS, 'django.forms.UUIDField')
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)