class RemoveDuplicatesListFieldModel(models.Model): char_list_field = ListField(models.CharField(max_length=20), remove_duplicates=True) int_list_field = ListField(models.IntegerField(max_length=20), remove_duplicates=True) dec_list_field = ListField(models.DecimalField(max_digits=6, decimal_places=2), remove_duplicates=True)
class Group(models.Model): """ This is a clone of django.contrib.auth.Group, but nonrelationalized. Doesn't user Permission but directly uses the permission names """ name = models.CharField(_('name'), max_length=80, unique=True) permissions = ListField(models.CharField(max_length=500), verbose_name=_('permissions'), blank=True) objects = GroupManager() class Meta: db_table = 'djangae_group' verbose_name = _('group') verbose_name_plural = _('groups') def __str__(self): return self.name def natural_key(self): return (self.name, ) def __init__(self, *args, **kwargs): """We need to override this to make the choices lazy and prevent import madness""" super(Group, self).__init__(*args, **kwargs) field = self._meta.get_field('permissions') field._choices = lazy(get_permission_choices, list)() field.item_field_type._choices = lazy(get_permission_choices, list)()
class Access(MetricsModel): timestamp = models.DateTimeField(auto_now_add=True, editable=False) session_key = models.CharField(max_length=40, null=True, editable=False) user = models.ForeignKey(USER_MODEL, null=True, editable=False) action = models.CharField(max_length=100, blank=False, editable=False) model = models.CharField(max_length=50, blank=False, editable=False) ids = ListField(models.CharField(max_length=100, editable=False)) objects = AccessManager()
def test_list_field(self): instance = IterableFieldModel.objects.create() self.assertEqual([], instance.list_field) instance.list_field.append("One") self.assertEqual(["One"], instance.list_field) instance.save() self.assertEqual(["One"], instance.list_field) instance = IterableFieldModel.objects.get(pk=instance.pk) self.assertEqual(["One"], instance.list_field) results = IterableFieldModel.objects.filter(list_field="One") self.assertEqual([instance], list(results)) self.assertEqual([1, 2], ListField(models.IntegerField).to_python("[1, 2]"))
class Group(models.Model): """ This is a clone of django.contrib.auth.Group, but nonrelationalized """ name = models.CharField(_('name'), max_length=80, unique=True) permissions = ListField(models.ForeignKey(Permission), verbose_name=_('permissions'), blank=True) objects = GroupManager() class Meta: verbose_name = _('group') verbose_name_plural = _('groups') def __str__(self): return self.name def natural_key(self): return (self.name, )
def test_list_field(self): instance = IterableFieldModel.objects.create() self.assertEqual([], instance.list_field) instance.list_field.append("One") self.assertEqual(["One"], instance.list_field) instance.save() self.assertEqual(["One"], instance.list_field) instance = IterableFieldModel.objects.get(pk=instance.pk) self.assertEqual(["One"], instance.list_field) instance.list_field = None # Or anything else for that matter! with self.assertRaises(ValueError): instance.list_field = "Bananas" instance.save() results = IterableFieldModel.objects.filter(list_field="One") self.assertEqual([instance], list(results)) self.assertEqual([1, 2], ListField(models.IntegerField).to_python("[1, 2]"))
class Group(models.Model): """ This is a clone of django.contrib.auth.Group, but nonrelationalized. Doesn't user Permission but directly uses the permission names """ name = models.CharField(_('name'), max_length=80, unique=True) permissions = ListField(models.CharField(max_length=500, choices=get_permission_choices()), verbose_name=_('permissions'), blank=True, choices=get_permission_choices()) objects = GroupManager() class Meta: verbose_name = _('group') verbose_name_plural = _('groups') app_label = "djangae" def __str__(self): return self.name def natural_key(self): return (self.name, )
class IterableFieldModel(models.Model): set_field = SetField(models.CharField(max_length=1)) list_field = ListField(models.CharField(max_length=1)) class Meta: app_label = "djangae"
class BlankableListFieldModel(models.Model): list_field = ListField(models.CharField(max_length=1), blank=True)
class PermissionsMixin(models.Model): """ A mixin class that adds the fields and methods necessary to support Django's Group and Permission model using the ModelBackend. """ is_superuser = models.BooleanField( _('superuser status'), default=False, help_text=_('Designates that this user has all permissions without ' 'explicitly assigning them.')) groups = RelatedSetField( Group, verbose_name=_('groups'), blank=True, help_text=_('The groups this user belongs to. A user will ' 'get all permissions granted to each of ' 'his/her group.')) user_permissions = ListField( models.CharField(max_length=500), verbose_name=_('user permissions'), blank=True, help_text='Specific permissions for this user.') class Meta: abstract = True def __init__(self, *args, **kwargs): """We need to override this to make the choices lazy and prevent import madness""" super(PermissionsMixin, self).__init__(*args, **kwargs) self._meta.get_field('user_permissions')._choices = lazy( get_permission_choices, list)() def get_group_permissions(self, obj=None): """ Returns a list of permission strings that this user has through his/her groups. This method queries all available auth backends. If an object is passed in, only permissions matching this object are returned. """ permissions = set() for backend in auth.get_backends(): if hasattr(backend, "get_group_permissions"): if obj is not None: permissions.update(backend.get_group_permissions( self, obj)) else: permissions.update(backend.get_group_permissions(self)) return permissions def get_all_permissions(self, obj=None): return _user_get_all_permissions(self, obj) def has_perm(self, perm, obj=None): """ Returns True if the user has the specified permission. This method queries all available auth backends, but returns immediately if any backend returns True. Thus, a user who has permission from a single auth backend is assumed to have permission in general. If an object is provided, permissions for this specific object are checked. """ # Active superusers have all permissions. if self.is_active and self.is_superuser: return True # Otherwise we need to check the backends. return _user_has_perm(self, perm, obj) def has_perms(self, perm_list, obj=None): """ Returns True if the user has each of the specified permissions. If object is passed, it checks if the user has all required perms for this object. """ for perm in perm_list: if not self.has_perm(perm, obj): return False return True def has_module_perms(self, app_label): """ Returns True if the user has any permissions in the given app label. Uses pretty much the same logic as has_perm, above. """ # Active superusers have all permissions. if self.is_active and self.is_superuser: return True return _user_has_module_perms(self, app_label)
class Recipe(models.Model): title = models.CharField(max_length=200) ingredients = ListField(models.CharField(max_length=50)) quantities = ListField(models.CharField(max_length=50)) method = models.TextField()
class WordSetLevel(models.Model): level = models.IntegerField() words = ListField(models.CharField(max_length=32)) class Meta: db_table = 'WordSetLevel'
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)