我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用django.db.models.SlugField()。
def clean(self): strip_types = (models.CharField, models.TextField, models.URLField, models.EmailField, models.IPAddressField, models.GenericIPAddressField, models.SlugField) for field in self._meta.fields: # TODO: hardcode 'notes' here if not (field.name is 'notes') and isinstance(field, strip_types): value = getattr(self, field.name) if value: setattr(self, field.name, value.replace('\t', ' ').replace('\n', ' ').replace('\r', ' '))
def deconstruct(self): name, path, args, kwargs = super(models.SlugField, self).deconstruct() kwargs['populate_from'] = self.populate_from if kwargs.get("max_length", None) == 64: del kwargs['max_length'] if self.blank is True: del kwargs['blank'] if self.help_text == DEFAULT_HELP: del kwargs['help_text'] if self.separator != '-': kwargs['separator'] = self.separator if self.default_slug != 'empty': kwargs['default_slug'] = self.default_slug if self.db_index is False: kwargs['db_index'] = False else: del kwargs['db_index'] return name, path, args, kwargs
def _categorization_translated_fields(): return TranslatedFields( name=models.CharField( _('Name'), max_length=128, ), slug=models.SlugField( _('Slug'), db_index=True, ), description=models.TextField( _('Description'), blank=True, help_text=_("Description of a categorization, usually used as lead text in categorization's detail view."), ), meta={ 'unique_together': [('language_code', 'slug')], }, )
def validate_field(self, errors, opts, f): """ MySQL has the following field length restriction: No character (varchar) fields can have a length exceeding 255 characters if they have a unique index on them. """ varchar_fields = (models.CharField, models.CommaSeparatedIntegerField, models.SlugField) if isinstance(f, varchar_fields) and f.max_length > 255 and f.unique: msg = ('"%(name)s": %(cls)s cannot have a "max_length" greater ' 'than 255 when using "unique=True".') errors.add(opts, msg % {'name': f.name, 'cls': f.__class__.__name__})
def get_internal_type(self): return "SlugField"
def test_should_slug_convert_string(): assert_conversion(models.SlugField, graphene.String)
def test_slug(self): field = Menu._meta.get_field('slug') self.assertModelField(field, models.SlugField, False, False)
def test_get_concrete_class_for_model_field(self): """ The get_concrete_class_for_model_field method should return the correct OmniField subclass """ self.assertEqual(OmniField.get_concrete_class_for_model_field(models.CharField()), OmniCharField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.NullBooleanField()), OmniBooleanField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.BooleanField()), OmniBooleanField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.DateTimeField()), OmniDateTimeField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.DecimalField()), OmniDecimalField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.EmailField()), OmniEmailField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.FloatField()), OmniFloatField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.IntegerField()), OmniIntegerField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.BigIntegerField()), OmniIntegerField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.PositiveIntegerField()), OmniIntegerField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.SmallIntegerField()), OmniIntegerField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.TimeField()), OmniTimeField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.URLField()), OmniUrlField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.SlugField()), OmniSlugField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.FileField()), OmniFileField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.ImageField()), OmniImageField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.DurationField()), OmniDurationField) self.assertEqual( OmniField.get_concrete_class_for_model_field(models.GenericIPAddressField()), OmniGenericIPAddressField ) self.assertEqual( OmniField.get_concrete_class_for_model_field(models.CommaSeparatedIntegerField()), OmniCharField ) self.assertEqual( OmniField.get_concrete_class_for_model_field(models.PositiveSmallIntegerField()), OmniIntegerField ) self.assertEqual( OmniField.get_concrete_class_for_model_field(models.ForeignKey(DummyModel2)), OmniForeignKeyField ) self.assertEqual( OmniField.get_concrete_class_for_model_field(models.ManyToManyField(DummyModel2)), OmniManyToManyField )
def test_initial(self): """ The model should have an initial field """ field = OmniSlugField._meta.get_field('initial') self.assertIsInstance(field, models.SlugField) self.assertTrue(field.blank) self.assertTrue(field.null)
def test_field_class(self): """ The model should define the correct field class """ self.assertEqual(OmniSlugField.FIELD_CLASS, 'django.forms.SlugField')
def get_concrete_class_for_model_field(cls, model_field): """ Method for getting a concrete model class to represent the type of form field required :param model_field: Model Field instance :return: OmniField subclass """ field_mapping = { models.CharField: OmniCharField, models.TextField: OmniCharField, models.BooleanField: OmniBooleanField, models.NullBooleanField: OmniBooleanField, models.DateField: OmniDateField, models.DateTimeField: OmniDateTimeField, models.DecimalField: OmniDecimalField, models.EmailField: OmniEmailField, models.FloatField: OmniFloatField, models.IntegerField: OmniIntegerField, models.BigIntegerField: OmniIntegerField, models.PositiveIntegerField: OmniIntegerField, models.PositiveSmallIntegerField: OmniIntegerField, models.SmallIntegerField: OmniIntegerField, models.CommaSeparatedIntegerField: OmniCharField, models.TimeField: OmniTimeField, models.URLField: OmniUrlField, models.ForeignKey: OmniForeignKeyField, models.ManyToManyField: OmniManyToManyField, models.SlugField: OmniSlugField, models.FileField: OmniFileField, models.ImageField: OmniImageField, models.DurationField: OmniDurationField, models.GenericIPAddressField: OmniGenericIPAddressField } field_mapping.update(cls.get_custom_field_mapping()) return field_mapping.get(model_field.__class__)