{"action":"created","comment":{"url":"https://api.github.com/repos/JBKahn/django-sharding/pulls/comments/64139622","id":64139622,"diff_hunk":"@@ -38,5 +39,24 @@ def configure(cls):\n             setattr(cls, 'django_sharding__shard_group', shard_group)\n             setattr(cls, 'django_sharding__is_sharded', True)\n \n+            # If the sharded by field is set, we will make our custom manager the default manager.\n+            # todo: update docs to indicate that ShardManager should be used as the base class for any custom managers->\n+            # for a model that defines a sharded by field\n+            # todo: Update docs to include the sharded by field and the get_shard_from_id function\n+            if sharded_by_field:\n+                from django.db.models import Manager","path":"django_sharding_library/decorators.py","position":21,"original_position":21,"commit_id":"e91868c64466fbf2c1e8c09d17b3019f06e66c09","original_commit_id":"e91868c64466fbf2c1e8c09d17b3019f06e66c09","user":{"login":"JBKahn","id":2654653,"avatar_url":"https://avatars.githubusercontent.com/u/2654653?v=3","gravatar_id":"","url":"https://api.github.com/users/JBKahn","html_url":"https://github.com/JBKahn","followers_url":"https://api.github.com/users/JBKahn/followers","following_url":"https://api.github.com/users/JBKahn/following{/other_user}","gists_url":"https://api.github.com/users/JBKahn/gists{/gist_id}","starred_url":"https://api.github.com/users/JBKahn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JBKahn/subscriptions","organizations_url":"https://api.github.com/users/JBKahn/orgs","repos_url":"https://api.github.com/users/JBKahn/repos","events_url":"https://api.github.com/users/JBKahn/events{/privacy}","received_events_url":"https://api.github.com/users/JBKahn/received_events","type":"User","site_admin":false},"body":"unless there is a circular import move it to the top?","created_at":"2016-05-21T20:10:54Z","updated_at":"2016-05-21T20:10:54Z","html_url":"https://github.com/JBKahn/django-sharding/pull/27#discussion_r64139622","pull_request_url":"https://api.github.com/repos/JBKahn/django-sharding/pulls/27","_links":{"self":{"href":"https://api.github.com/repos/JBKahn/django-sharding/pulls/comments/64139622"},"html":{"href":"https://github.com/JBKahn/django-sharding/pull/27#discussion_r64139622"},"pull_request":{"href":"https://api.github.com/repos/JBKahn/django-sharding/pulls/27"}}},"pull_request":{"url":"https://api.github.com/repos/JBKahn/django-sharding/pulls/27","id":68728763,"html_url":"https://github.com/JBKahn/django-sharding/pull/27","diff_url":"https://github.com/JBKahn/django-sharding/pull/27.diff","patch_url":"https://github.com/JBKahn/django-sharding/pull/27.patch","issue_url":"https://api.github.com/repos/JBKahn/django-sharding/issues/27","number":27,"state":"open","locked":false,"title":"Automatic db selection from filter() and create() arguments","user":{"login":"4word","id":1970618,"avatar_url":"https://avatars.githubusercontent.com/u/1970618?v=3","gravatar_id":"","url":"https://api.github.com/users/4word","html_url":"https://github.com/4word","followers_url":"https://api.github.com/users/4word/followers","following_url":"https://api.github.com/users/4word/following{/other_user}","gists_url":"https://api.github.com/users/4word/gists{/gist_id}","starred_url":"https://api.github.com/users/4word/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/4word/subscriptions","organizations_url":"https://api.github.com/users/4word/orgs","repos_url":"https://api.github.com/users/4word/repos","events_url":"https://api.github.com/users/4word/events{/privacy}","received_events_url":"https://api.github.com/users/4word/received_events","type":"User","site_admin":false},"body":"This PR introduces automated routing to the correct database based on the kwargs passed in filter(), get(), create(), and get_or_create() by inspecting the arguments and getting the shard automatically using a static method called get_shard_from_id() on the model. It requires that a sharded_by_field be passed to the decorator model_config, and it requires the model to inherit from the abstract model ShardModel.\r\n\r\nExample:\r\n```\r\n@model_config(shard_group='default', sharded_by_field='user_pk')\r\nclass TestModel(ShardModel):\r\n    id = TableShardedIDField(primary_key=True, source_table=ShardedTestModelIDs)\r\n    random_string = models.CharField(max_length=120)\r\n    user_pk = models.PositiveIntegerField()\r\n\r\n    def get_shard(self):\r\n        from django.contrib.auth import get_user_model\r\n        return get_user_model().objects.get(pk=self.user_pk).shard\r\n\r\n    @staticmethod\r\n    def get_shard_from_id(user_pk):\r\n        from django.contrib.auth import get_user_model\r\n        return get_user_model().objects.get(pk=user_pk).shard\r\n\r\ntest_object = TestModel.objects.create(user_pk=1, random_strin=\"test\")\r\nassert test_object._state.db == User.objects.get(pk=1).shard\r\n```\r\n\r\nThe shard will be automatically selected as long as the \"sharded_by_id\" is passed in.","created_at":"2016-05-03T15:51:55Z","updated_at":"2016-05-21T20:10:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2c7f0adc450fc97f8798d070cb2f8bbd5f37410c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/JBKahn/django-sharding/pulls/27/commits","review_comments_url":"https://api.github.com/repos/JBKahn/django-sharding/pulls/27/comments","review_comment_url":"https://api.github.com/repos/JBKahn/django-sharding/pulls/comments{/number}","comments_url":"https://api.github.com/repos/JBKahn/django-sharding/issues/27/comments","statuses_url":"https://api.github.com/repos/JBKahn/django-sharding/statuses/e91868c64466fbf2c1e8c09d17b3019f06e66c09","head":{"label":"4word:shard-by-manager-and-router","ref":"shard-by-manager-and-router","sha":"e91868c64466fbf2c1e8c09d17b3019f06e66c09","user":{"login":"4word","id":1970618,"avatar_url":"https://avatars.githubusercontent.com/u/1970618?v=3","gravatar_id":"","url":"https://api.github.com/users/4word","html_url":"https://github.com/4word","followers_url":"https://api.github.com/users/4word/followers","following_url":"https://api.github.com/users/4word/following{/other_user}","gists_url":"https://api.github.com/users/4word/gists{/gist_id}","starred_url":"https://api.github.com/users/4word/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/4word/subscriptions","organizations_url":"https://api.github.com/users/4word/orgs","repos_url":"https://api.github.com/users/4word/repos","events_url":"https://api.github.com/users/4word/events{/privacy}","received_events_url":"https://api.github.com/users/4word/received_events","type":"User","site_admin":false},"repo":{"id":57902718,"name":"django-sharding","full_name":"4word/django-sharding","owner":{"login":"4word","id":1970618,"avatar_url":"https://avatars.githubusercontent.com/u/1970618?v=3","gravatar_id":"","url":"https://api.github.com/users/4word","html_url":"https://github.com/4word","followers_url":"https://api.github.com/users/4word/followers","following_url":"https://api.github.com/users/4word/following{/other_user}","gists_url":"https://api.github.com/users/4word/gists{/gist_id}","starred_url":"https://api.github.com/users/4word/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/4word/subscriptions","organizations_url":"https://api.github.com/users/4word/orgs","repos_url":"https://api.github.com/users/4word/repos","events_url":"https://api.github.com/users/4word/events{/privacy}","received_events_url":"https://api.github.com/users/4word/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/4word/django-sharding","description":"A sharding library for Django","fork":true,"url":"https://api.github.com/repos/4word/django-sharding","forks_url":"https://api.github.com/repos/4word/django-sharding/forks","keys_url":"https://api.github.com/repos/4word/django-sharding/keys{/key_id}","collaborators_url":"https://api.github.com/repos/4word/django-sharding/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/4word/django-sharding/teams","hooks_url":"https://api.github.com/repos/4word/django-sharding/hooks","issue_events_url":"https://api.github.com/repos/4word/django-sharding/issues/events{/number}","events_url":"https://api.github.com/repos/4word/django-sharding/events","assignees_url":"https://api.github.com/repos/4word/django-sharding/assignees{/user}","branches_url":"https://api.github.com/repos/4word/django-sharding/branches{/branch}","tags_url":"https://api.github.com/repos/4word/django-sharding/tags","blobs_url":"https://api.github.com/repos/4word/django-sharding/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/4word/django-sharding/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/4word/django-sharding/git/refs{/sha}","trees_url":"https://api.github.com/repos/4word/django-sharding/git/trees{/sha}","statuses_url":"https://api.github.com/repos/4word/django-sharding/statuses/{sha}","languages_url":"https://api.github.com/repos/4word/django-sharding/languages","stargazers_url":"https://api.github.com/repos/4word/django-sharding/stargazers","contributors_url":"https://api.github.com/repos/4word/django-sharding/contributors","subscribers_url":"https://api.github.com/repos/4word/django-sharding/subscribers","subscription_url":"https://api.github.com/repos/4word/django-sharding/subscription","commits_url":"https://api.github.com/repos/4word/django-sharding/commits{/sha}","git_commits_url":"https://api.github.com/repos/4word/django-sharding/git/commits{/sha}","comments_url":"https://api.github.com/repos/4word/django-sharding/comments{/number}","issue_comment_url":"https://api.github.com/repos/4word/django-sharding/issues/comments{/number}","contents_url":"https://api.github.com/repos/4word/django-sharding/contents/{+path}","compare_url":"https://api.github.com/repos/4word/django-sharding/compare/{base}...{head}","merges_url":"https://api.github.com/repos/4word/django-sharding/merges","archive_url":"https://api.github.com/repos/4word/django-sharding/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/4word/django-sharding/downloads","issues_url":"https://api.github.com/repos/4word/django-sharding/issues{/number}","pulls_url":"https://api.github.com/repos/4word/django-sharding/pulls{/number}","milestones_url":"https://api.github.com/repos/4word/django-sharding/milestones{/number}","notifications_url":"https://api.github.com/repos/4word/django-sharding/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/4word/django-sharding/labels{/name}","releases_url":"https://api.github.com/repos/4word/django-sharding/releases{/id}","deployments_url":"https://api.github.com/repos/4word/django-sharding/deployments","created_at":"2016-05-02T16:09:30Z","updated_at":"2016-05-02T16:09:31Z","pushed_at":"2016-05-19T23:41:09Z","git_url":"git://github.com/4word/django-sharding.git","ssh_url":"git@github.com:4word/django-sharding.git","clone_url":"https://github.com/4word/django-sharding.git","svn_url":"https://github.com/4word/django-sharding","homepage":null,"size":651,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"JBKahn:master","ref":"master","sha":"534d1fdf907ee7c95afc42b92d82657ba258a345","user":{"login":"JBKahn","id":2654653,"avatar_url":"https://avatars.githubusercontent.com/u/2654653?v=3","gravatar_id":"","url":"https://api.github.com/users/JBKahn","html_url":"https://github.com/JBKahn","followers_url":"https://api.github.com/users/JBKahn/followers","following_url":"https://api.github.com/users/JBKahn/following{/other_user}","gists_url":"https://api.github.com/users/JBKahn/gists{/gist_id}","starred_url":"https://api.github.com/users/JBKahn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JBKahn/subscriptions","organizations_url":"https://api.github.com/users/JBKahn/orgs","repos_url":"https://api.github.com/users/JBKahn/repos","events_url":"https://api.github.com/users/JBKahn/events{/privacy}","received_events_url":"https://api.github.com/users/JBKahn/received_events","type":"User","site_admin":false},"repo":{"id":44042305,"name":"django-sharding","full_name":"JBKahn/django-sharding","owner":{"login":"JBKahn","id":2654653,"avatar_url":"https://avatars.githubusercontent.com/u/2654653?v=3","gravatar_id":"","url":"https://api.github.com/users/JBKahn","html_url":"https://github.com/JBKahn","followers_url":"https://api.github.com/users/JBKahn/followers","following_url":"https://api.github.com/users/JBKahn/following{/other_user}","gists_url":"https://api.github.com/users/JBKahn/gists{/gist_id}","starred_url":"https://api.github.com/users/JBKahn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JBKahn/subscriptions","organizations_url":"https://api.github.com/users/JBKahn/orgs","repos_url":"https://api.github.com/users/JBKahn/repos","events_url":"https://api.github.com/users/JBKahn/events{/privacy}","received_events_url":"https://api.github.com/users/JBKahn/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/JBKahn/django-sharding","description":"A sharding library for Django","fork":false,"url":"https://api.github.com/repos/JBKahn/django-sharding","forks_url":"https://api.github.com/repos/JBKahn/django-sharding/forks","keys_url":"https://api.github.com/repos/JBKahn/django-sharding/keys{/key_id}","collaborators_url":"https://api.github.com/repos/JBKahn/django-sharding/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/JBKahn/django-sharding/teams","hooks_url":"https://api.github.com/repos/JBKahn/django-sharding/hooks","issue_events_url":"https://api.github.com/repos/JBKahn/django-sharding/issues/events{/number}","events_url":"https://api.github.com/repos/JBKahn/django-sharding/events","assignees_url":"https://api.github.com/repos/JBKahn/django-sharding/assignees{/user}","branches_url":"https://api.github.com/repos/JBKahn/django-sharding/branches{/branch}","tags_url":"https://api.github.com/repos/JBKahn/django-sharding/tags","blobs_url":"https://api.github.com/repos/JBKahn/django-sharding/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/JBKahn/django-sharding/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/JBKahn/django-sharding/git/refs{/sha}","trees_url":"https://api.github.com/repos/JBKahn/django-sharding/git/trees{/sha}","statuses_url":"https://api.github.com/repos/JBKahn/django-sharding/statuses/{sha}","languages_url":"https://api.github.com/repos/JBKahn/django-sharding/languages","stargazers_url":"https://api.github.com/repos/JBKahn/django-sharding/stargazers","contributors_url":"https://api.github.com/repos/JBKahn/django-sharding/contributors","subscribers_url":"https://api.github.com/repos/JBKahn/django-sharding/subscribers","subscription_url":"https://api.github.com/repos/JBKahn/django-sharding/subscription","commits_url":"https://api.github.com/repos/JBKahn/django-sharding/commits{/sha}","git_commits_url":"https://api.github.com/repos/JBKahn/django-sharding/git/commits{/sha}","comments_url":"https://api.github.com/repos/JBKahn/django-sharding/comments{/number}","issue_comment_url":"https://api.github.com/repos/JBKahn/django-sharding/issues/comments{/number}","contents_url":"https://api.github.com/repos/JBKahn/django-sharding/contents/{+path}","compare_url":"https://api.github.com/repos/JBKahn/django-sharding/compare/{base}...{head}","merges_url":"https://api.github.com/repos/JBKahn/django-sharding/merges","archive_url":"https://api.github.com/repos/JBKahn/django-sharding/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/JBKahn/django-sharding/downloads","issues_url":"https://api.github.com/repos/JBKahn/django-sharding/issues{/number}","pulls_url":"https://api.github.com/repos/JBKahn/django-sharding/pulls{/number}","milestones_url":"https://api.github.com/repos/JBKahn/django-sharding/milestones{/number}","notifications_url":"https://api.github.com/repos/JBKahn/django-sharding/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/JBKahn/django-sharding/labels{/name}","releases_url":"https://api.github.com/repos/JBKahn/django-sharding/releases{/id}","deployments_url":"https://api.github.com/repos/JBKahn/django-sharding/deployments","created_at":"2015-10-11T06:53:56Z","updated_at":"2016-05-17T13:31:31Z","pushed_at":"2016-05-21T19:34:22Z","git_url":"git://github.com/JBKahn/django-sharding.git","ssh_url":"git@github.com:JBKahn/django-sharding.git","clone_url":"https://github.com/JBKahn/django-sharding.git","svn_url":"https://github.com/JBKahn/django-sharding","homepage":null,"size":663,"stargazers_count":16,"watchers_count":16,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":5,"mirror_url":null,"open_issues_count":12,"forks":5,"open_issues":12,"watchers":16,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/JBKahn/django-sharding/pulls/27"},"html":{"href":"https://github.com/JBKahn/django-sharding/pull/27"},"issue":{"href":"https://api.github.com/repos/JBKahn/django-sharding/issues/27"},"comments":{"href":"https://api.github.com/repos/JBKahn/django-sharding/issues/27/comments"},"review_comments":{"href":"https://api.github.com/repos/JBKahn/django-sharding/pulls/27/comments"},"review_comment":{"href":"https://api.github.com/repos/JBKahn/django-sharding/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/JBKahn/django-sharding/pulls/27/commits"},"statuses":{"href":"https://api.github.com/repos/JBKahn/django-sharding/statuses/e91868c64466fbf2c1e8c09d17b3019f06e66c09"}}}}
{"actor":{"display_login":"JBKahn"}}