Skip to content

Conversation

@kevinetienne
Copy link
Contributor

Customising fields on BasicUserFieldsMixin is not easy. I have a case where I need to override
name and email.

My use case:

class User(AvatarMixin, VerifyEmailMixin, PermissionsMixin, AbstractBaseUser):
    pass

With the current implementation the solution is to copy VerifyEmailMixin and its parent class
BasicUserFieldsMixin to redefine name and email fields to avoid name clashes.

It looks like splitting the current mixins to more mixins will allow redefining fields more easily.
I suggest to make BasicUserFieldsMixin and VerifyEmailMixin children classes of some fields and methods.

Example:

class BasicUserFieldsMixin(IsStaffMixin, NameMixin, NameMethodsMixin):
    pass

class VerifyEmailMixin(BasicUserFieldsMixin, IsActiveMixin, EmailVerificationMixin, VerifyEmailMethodsMixin):
   pass

Which would allow us to create own BasicUserFieldsMixin and VerifyEmailMixin if one or more fields need changing.

@kevinetienne
Copy link
Contributor Author

Having such a long chain of inheritance is maybe not the best solution, dealing with python mro and Django Meta class has proven not to be easy to use.

One of the downside of what I have created is that one should reimplement the checks when customising VerifyEmailMixin. Thanks @ian-foote

@kevinetienne
Copy link
Contributor Author

@ian-foote @meshy could you have a look to this PR please?

@kevinetienne kevinetienne force-pushed the split-mixins-into-mixins branch from fdd3ce8 to 179bf9e Compare November 27, 2014 16:45
@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) when pulling 179bf9e on split-mixins-into-mixins into cf607ed on master.

@meshy
Copy link
Contributor

meshy commented Nov 27, 2014

Just to clarify: it's not possible to just subclass the parent, and re-define the fields?

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) when pulling 6f431b9 on split-mixins-into-mixins into cf607ed on master.

@kevinetienne
Copy link
Contributor Author

@meshy unfortunately no :( The error message is the following: django.core.exceptions.FieldError: Local field 'name' in class 'User' clashes with field of similar name from base class 'VerifyEmailMixin' and then you try to subclass VerifyEmailMixin but same error :)

@kevinetienne
Copy link
Contributor Author

it might be related to the Meta class generating something from the attributes on the class.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) when pulling 6f431b9 on split-mixins-into-mixins into cf607ed on master.

@meshy
Copy link
Contributor

meshy commented Nov 27, 2014

Aaah, I guess it is. Shame. Ok then :)

@kevinetienne
Copy link
Contributor Author

@kevinetienne kevinetienne force-pushed the split-mixins-into-mixins branch from ec6f589 to 41eabc3 Compare November 28, 2014 10:24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this one for?

@kevinetienne kevinetienne force-pushed the split-mixins-into-mixins branch 3 times, most recently from fe48983 to 39831d6 Compare November 28, 2014 10:44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this syntax :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If feels weird and it leaves a ): on a new line, flake8 enforce this syntax..

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative is:

basic_user_mixins = (
    NameUserMethodsMixin,
    EmailUserMixin,
    DateJoinedUserMixin,
    IsStaffUserMixin,
)

class CustomBasicUserFieldsMixin(*basic_user_mixins):
    ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ian-foote I like this one a lot better

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ): does not need to be on new line tho

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately the syntax with the splat produces an invalid syntax error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who do you respond to? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my last message was for @ian-foote

@kevinetienne kevinetienne force-pushed the split-mixins-into-mixins branch from 60df2b2 to d1e4b30 Compare November 28, 2014 12:39
@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) when pulling d1e4b30 on split-mixins-into-mixins into 8c4649a on master.

@kevinetienne
Copy link
Contributor Author

@ian-foote @mattack108 can you merge please?

@kevinetienne kevinetienne force-pushed the split-mixins-into-mixins branch from d1e4b30 to f9d1749 Compare November 28, 2014 16:15
LilyFirefly pushed a commit that referenced this pull request Nov 28, 2014
Split mixins between fields and methods
@LilyFirefly LilyFirefly merged commit 8b7f485 into master Nov 28, 2014
@LilyFirefly LilyFirefly deleted the split-mixins-into-mixins branch November 28, 2014 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants