我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用django.db.models.URLField()。
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 test_model_destination_url_field(self): """Test model 'destination_url' field""" field = self.model._meta.get_field('destination_url') assert isinstance(field, models.URLField) assert field.verbose_name == 'destination url'
def formfield(self, **kwargs): defaults = { 'form_class': forms.ImageURLField, } defaults.update(kwargs) return super(URLField, self).formfield(**defaults)
def GitURLField(**kwargs): r = models.URLField(**kwargs) for i in range(len(r.validators)): if isinstance(r.validators[i], validators.URLValidator): r.validators[i] = GitURLValidator() return r
def test_should_url_convert_string(): assert_conversion(models.URLField, graphene.String)
def test_link_external(self): field = AbstractLinkFields._meta.get_field('link_external') self.assertModelField(field, models.URLField, False, True)
def from_field(cls, field): if isinstance(field, models.URLField): return cls(verbose_name=title(field.verbose_name))
def create_schedule_abc(user_model=None): """Abstract base schedule model factory. :param user_model: User model to use as schedule owners. :type user_model: :class:`~news.models.AbstractModel` implemenatation :returns: A abstract base schedule model. :rtype: Abstract base django model of :class:`~news.models.AbstractSchedule` implementation. """ user_model = user_model or settings.AUTH_USER_MODEL class AbstractBaseSchedule(models.Model, AbstractSchedule): owner = models.ForeignKey( user_model, related_name='schedules', db_index=True, blank=True, null=True, ) url = models.URLField() cycle = models.IntegerField(default=DEFAULT_SCHEDULE_CYCLE) enabled = models.BooleanField(default=False) type = models.CharField(max_length=SCHEDULE_TYPE_MAX_LENGTH, default=DEFAULT_SCHEDULE_TYPE) options = JSONField(default=DEFAULT_OPTIONS) class Meta: abstract = True unique_together = (('owner', 'url'),) return AbstractBaseSchedule
def create_news_abc(schedule_model): """Abstract base news model factory. :param schedule_model: Schedule model to use as news's schedule. :type schedule_model: Any concrete schedule model of abc models from :func:`~create_abc_schedule` factory function. :returns: A abstract base news model. :rtype: Abstract base django model of :class:`~news.models.AbstractNews` implementation """ class AbstractBaseNews(models.Model, AbstractNews): schedule = models.ForeignKey( schedule_model, related_name='news_list', db_index=True ) parent = models.ForeignKey( 'self', related_name='children', db_index=True, blank=True, null=True ) url = models.URLField() author = models.CharField(max_length=AUTHOR_MAX_LENGTH, null=True) title = models.CharField(max_length=TITLE_MAX_LENGTH) summary = models.TextField() content = models.TextField() image = models.URLField(null=True) published = models.DateTimeField(blank=True, null=True) created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) class Meta: abstract = True unique_together = (('schedule', 'url'),) return AbstractBaseNews
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 = OmniUrlField._meta.get_field('initial') self.assertIsInstance(field, models.URLField) self.assertTrue(field.blank) self.assertTrue(field.null)
def test_field_class(self): """ The model should define the correct field class """ self.assertEqual(OmniUrlField.FIELD_CLASS, 'django.forms.URLField')
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__)
def __unicode__(self): return self.id #--------------------------------------------------------# # class Ranking(models.Model): # app = models.ForeignKey(App) #category is included in app # type = models.CharField(max_length=20) #free, paid, grossing # rank = models.IntegerField(blank=True, null=True) # date = models.DateTimeField() # # crawled_on = models.DateTimeField(auto_now_add=True) # # def __unicode__(self): # return self.id # class Rating(models.Model): # app = models.ForeignKey(App) # date = models.DateTimeField() # five_stars = models.BigIntegerField(default=0) # four_stars = models.BigIntegerField(default=0) # three_stars = models.BigIntegerField(default=0) # two_stars = models.BigIntegerField(default=0) # one_stars = models.BigIntegerField(default=0) # # crawled_on = models.DateTimeField(auto_now_add=True) # # def __unicode__(self): # return self.id # class User(models.Model): # username = models.CharField(max_length=200) # user_apple_id = models.BigIntegerField(default=0) # user_reviews_url = models.URLField(blank=True, null=True) # # We could use an M2M structure to connect reviews to users; # # but for the sake of simplicity we didn't use it. # def __unicode__(self): # return self.id # class Review(models.Model): # app = models.ForeignKey(App) # appfigures_id = models.IntegerField(blank=True, null=True) # user = models.ForeignKey(User) # review_id = models.BigIntegerField(default=0) # title = models.CharField(max_length=400) # body = models.TextField(blank=True, null=True) # star_rating = models.IntegerField(default=0) # date = models.DateTimeField() # # crawled_on = models.DateTimeField(auto_now_add=True) # def __unicode__(self): # return self.id