New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why does Organization have the user ManyToManyField? #25
Comments
Ok, I discovered the difference between Organization.users and Organization.organization_users. The users ManyToManyField actually selects the User model, and queries "through" a join to the OrganizationUser model, whereas organization_users just selects OrganizationUser model without retrieving the User model directly. So I retract my statement that it is redundant. However, it is still not compatible with Django-NonRel. A lot of popular Django packages are designed to work with both regular Django and the nonrel version. Are you willing to have django-organizations also be compatible? The existing usage scenarios of users is limited, so I think any possible performance impact is negligible. It's used in 4 places:
def is_member(self, user):
return next((ou for ou in self.organization_users.all() if ou.user_id == user.id), False) and True
|
I don't think it's a question of whether I'm willing for it to be compatible. The answer to that is 'certainly'. If the question is whether I'm willing to break the interface for existing projects to make it compatible with non-relational databases then the answer right now is "no". The ManyToManyField is pretty useful and I know of a few projects, including those I've worked on, that make use of that field in the project code itself. It's an exposed interface. I'm not yet willing to break that to support non-relational databases. |
Thanks, I totally understand. What if models.py only adds the ManyToManyField if it's not nonrel, and then remove internal usage of it? |
How do you feel with adding the following to models.py to solve this issue? from django.db import connection, models
if connection.features.supports_joins:
# this is the case for typical relational sql db
class AbstractOrganizationBase(TimeStampedModel):
users = models.ManyToManyField(USER_MODEL, through="OrganizationUser")
class Meta:
abstract = True
else:
# this is typically the NoSQL Django-nonrel case (e.g., mongodb or gae)
AbstractOrganizationBase = TimeStampedModel It would also require replacing the existing internal references to the users field with alternative solutions that work without the users field. I already implemented the alternative solutions in my fork; it's no big deal. Developers can still continue to use the users field as normal, assuming they are using a relational db. |
It seems like a reasonable way of doing it. I'd need try it and see some tests before including it. |
(1) Make django-nonrel compatible - bennylope#25 (2) Make organization and organizationuser customizable - bennylope#28
Thanks. Here is a fork diff: https://github.com/jacobg/django-organizations/compare |
(1) Make django-nonrel compatible - bennylope#25 (2) Make organization and organizationuser customizable - bennylope#23
I'm curious about the
I cannot find any reference to What indication do you have that this works? |
Hi Ben, You're right. I realize now that support_joins was added in the default db backend only for django-nonrel. I mistakenly assumed it is in the main django repository as well, because nonrel makes very minimal changes to the django source code. The primary changes are in separate apps. In any event, the code check in models.py should probably read: if getattr(connection.features, 'supports_joins', True): That is, if the supports_joins attribute does not exist, this means it's regular django and joins are supported. If it exists, then it's django-nonrel and then we check its value. It could be True, because you can use a regular relational db backend with nonrel. |
Closing this - at this point it looks like allowing non-rel functionality is going to take a lot more than toggling this one field. |
It seems that the related_name on OrganizationUser (i.e., organization_users) which points back to Organization accomplishes the same thing, and in fact self.organization_users is used in some places in the code in place of self.users (see Organization.is_admin). Sorry, I have not gone through actually fully testing what I'm saying, as I am using Django-nonrel and ManyToManyField is not supported at all. But it does appear that using related_name on a ForeignKey is identical to using through on ManyToManyField from the other side of the relationship.
Can you eliminate ManyToManyField to make it nonrel-friendly, and in general to remove redundant fields?
Thanks!
The text was updated successfully, but these errors were encountered: