class IterableFieldModel(models.Model): set_field = SetField(models.CharField(max_length=1)) list_field = ListField(models.CharField(max_length=1)) set_field_int = SetField(models.BigIntegerField(max_length=1)) list_field_int = ListField(models.BigIntegerField(max_length=1)) set_field_dt = SetField(models.DateTimeField()) list_field_dt = ListField(models.DateTimeField()) class Meta: app_label = "gcloudc"
def test_all_fields_are_pickleable(self): """ In order to work with Djangae's migrations, all fields must be pickeable. """ fields = [ CharField(), CharOrNoneField(), ComputedBooleanField("method_name"), ComputedCharField("method_name"), ComputedIntegerField("method_name"), ComputedPositiveIntegerField("method_name"), ComputedTextField("method_name"), GenericRelationField(), JSONField(default=list), ListField(CharField(), default=["badger"]), SetField(CharField(), default=set(["badger"])), ] fields.extend([ RelatedListField(ModelWithCharField), RelatedSetField(ModelWithCharField) ]) for field in fields: try: pickle.dumps(field) except (pickle.PicklingError, TypeError) as e: self.fail("Could not pickle %r: %s" % (field, e))
class OAuthUserSession(models.Model): id = models.CharField(max_length=21, primary_key=True) access_token = models.CharField(max_length=150, blank=True) refresh_token = models.CharField(max_length=150, blank=True) id_token = models.CharField(max_length=1500, blank=True) token_type = models.CharField(max_length=150, blank=True) expires_at = models.DateTimeField() scopes = SetField(models.CharField(max_length=1500), blank=True) token = JSONField(blank=True) # The returned profile data from the last refresh profile = JSONField(blank=True) # Related Django user (if any) def user(self): return User.objects.filter(google_oauth_id=self.pk).first() @property def is_valid(self): return timezone.now() < self.expires_at def refresh(self): pass
class Group(models.Model): name = models.CharField(_('name'), max_length=150, unique=True) permissions = SetField( PermissionChoiceField(), blank=True ) def __str__(self): return self.name
def test_set_field(self): instance = IterableFieldModel.objects.create() self.assertEqual(set(), instance.set_field) instance.set_field.add("One") self.assertEqual(set(["One"]), instance.set_field) instance.save() self.assertEqual(set(["One"]), instance.set_field) instance = IterableFieldModel.objects.get(pk=instance.pk) self.assertEqual(set(["One"]), instance.set_field) self.assertEqual({1, 2}, SetField(models.IntegerField).to_python("[1, 2]"))
class IterableFieldsWithValidatorsModel(models.Model): set_field = SetField(models.CharField(max_length=100), min_length=2, max_length=3, blank=False) list_field = ListField(models.CharField(max_length=100), min_length=2, max_length=3, blank=False) related_set = RelatedSetField(ISOther, min_length=2, max_length=3, blank=False) related_list = RelatedListField(ISOther, related_name="iterable_list", min_length=2, max_length=3, blank=False)
class UniqueModel(models.Model): unique_field = models.CharField(max_length=100, unique=True) unique_combo_one = models.IntegerField(blank=True, default=0) unique_combo_two = models.CharField(max_length=100, blank=True, default="") unique_relation = models.ForeignKey('self', null=True, blank=True, unique=True, on_delete=models.DO_NOTHING) unique_set_field = SetField(models.CharField(max_length=500), unique=True) unique_list_field = ListField(models.CharField(max_length=500), unique=True) unique_together_list_field = ListField(models.IntegerField()) class Meta: unique_together = [("unique_combo_one", "unique_combo_two"), ("unique_together_list_field", "unique_combo_one")]