Skip to content

Commit 24c3f31

Browse files
committed
Fix for Instance of 'ManyToManyField' has no 'add' member. Closes #163
Disable model-no-explicit-unicode for the test class because Django 1.11 defines the AbstractBaseUser with @python_2_unicode_compatible and we don't care about this issue for this particular test.
1 parent cfdbfde commit 24c3f31

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

pylint_django/augmentations/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@
244244

245245

246246
MANYTOMANY_FIELD_ATTRS = {
247+
'add',
248+
'clear',
247249
'related_name',
248250
'related_query_name',
249251
'limit_choices_to',

pylint_django/tests/input/func_noerror_manytomanyfield.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
# pylint: disable=missing-docstring
66
from django.db import models
7+
from django.contrib.auth.models import AbstractUser, Permission
78

89

910
class Book(models.Model):
@@ -24,3 +25,28 @@ def is_author_of(self, book):
2425

2526
def wrote_how_many(self):
2627
return self.wrote.count()
28+
29+
30+
# Custom permissions for CustomUser
31+
USER_PERMS = ['change_customuser', 'add_customuser']
32+
33+
34+
class CustomUser(AbstractUser): # pylint: disable=model-no-explicit-unicode
35+
class Meta:
36+
verbose_name = 'CustomUser'
37+
verbose_name_plural = 'CustomUsers'
38+
app_label = "users"
39+
40+
def grant_permissions(self):
41+
''' Example adding permissions to User '''
42+
self.user_permissions.clear()
43+
for perm in USER_PERMS:
44+
perm = Permission.objects.get(codename=perm)
45+
self.user_permissions.add(perm)
46+
return self.user_permissions
47+
48+
def save(self, *args, **kwargs):
49+
''' Saving while granting new permissions '''
50+
self.is_staff = True
51+
super(CustomUser, self).save()
52+
self.grant_permissions()

0 commit comments

Comments
 (0)