{"id":13500806,"url":"https://github.com/arthanson/django-genericadmin","last_synced_at":"2025-04-05T12:06:51.377Z","repository":{"id":49977005,"uuid":"2622183","full_name":"arthanson/django-genericadmin","owner":"arthanson","description":"A simple django app to make the lookup of generic models easier.","archived":false,"fork":false,"pushed_at":"2024-03-06T19:16:55.000Z","size":100,"stargazers_count":109,"open_issues_count":12,"forks_count":87,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-03-15T03:42:36.971Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arthanson.png","metadata":{"files":{"readme":"README.markdown","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-10-21T18:19:08.000Z","updated_at":"2024-06-19T00:10:31.798Z","dependencies_parsed_at":"2024-06-19T00:10:29.681Z","dependency_job_id":"f8b7960c-b687-440a-9da3-3e0248989bca","html_url":"https://github.com/arthanson/django-genericadmin","commit_stats":{"total_commits":85,"total_committers":20,"mean_commits":4.25,"dds":0.5529411764705883,"last_synced_commit":"b4916ea9421a484e893543bd1cbc6edc1c27fa66"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arthanson%2Fdjango-genericadmin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arthanson%2Fdjango-genericadmin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arthanson%2Fdjango-genericadmin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arthanson%2Fdjango-genericadmin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arthanson","download_url":"https://codeload.github.com/arthanson/django-genericadmin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247332606,"owners_count":20921853,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T22:01:14.868Z","updated_at":"2025-04-05T12:06:51.351Z","avatar_url":"https://github.com/arthanson.png","language":"JavaScript","readme":"# django-genericadmin\n\nA simple django app to make the lookup of generic models easier. \n\n## Installation\n\nRun the usual\n\n```pip install django-genericadmin```\n\nand add it to your `INSTALLED_APPS` in your project's `settings.py`. There is no need to run `manage.py syncdb` or `manage.py migrate` because _django-genericadmin_ does not have any models.\n\n```python\nINSTALLED_APPS = (\n   ...\n   'genericadmin',\n   ...\n)\n```\n\nIf you are using the staticfiles app, then run `manage.py collectstatic` and you should be good to go. \n\nIf you don't know what I'm talking about or your django version \u003c 1.3, then you should link or copy `genericadmin/media/js/` to your asset directory and set `GENERICADMIN_JS` to a the relative destination of your just copied files. \n\n## Usage\n\nTo use _django-genericadmin_ your model admin class must inherit from `GenericAdminModelAdmin`. \n\nSo a model admin like\n\n```python\nclass NavBarEntryAdmin(admin.ModelAdmin):\n    pass\n\nadmin.site.register(NavBarEntry, NavBarEntryAdmin)\n```\n\nbecomes\n\n```python\nfrom genericadmin.admin import GenericAdminModelAdmin\n\nclass NavBarEntryAdmin(GenericAdminModelAdmin):\n    pass\n\nadmin.site.register(NavBarEntry, NavBarEntryAdmin)\n```\n\nThat's it.\n\n## Provided admin classes\n\nA short overview of the admin classes and their uses provided by  _django-genericadmin_.\n\n * __GenericAdminModelAdmin__ \u0026mdash; The admin for a standard Django model that has at least one generic foreign relation.\n\n * __TabularInlineWithGeneric__ and __StackedInlineWithGeneric__ \u0026mdash; Normal inline admins for models that have a generic relation and are edited inline.\n\n\n * __GenericTabularInline__ and __GenericStackedInline__ \u0026mdash; Used to provide _True Polymorphic Relationships_ (see below) and generic relations in the admin. Also see the Django docs [here](https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#generic-relations-in-forms-and-admin).\n\n\n## Inline Usage\n\nTo use _django-genericadmin_ with admin inlines, your models must inherit from `GenericAdminModelAdmin` as described above:\n\n```python\nfrom genericadmin.admin import GenericAdminModelAdmin\n\nclass NavBarEntryAdmin(GenericAdminModelAdmin):\n   \tpass\n\nadmin.site.register(NavBarEntry, NavBarEntryAdmin)\n```\n\nAdditionally the inline classes must inherit from either `StackedInlineWithGeneric` or `TabularInlineWithGeneric`:\n\n```python\nfrom genericadmin.admin import GenericAdminModelAdmin, TabularInlineWithGeneric\n\nclass PagesInline(TabularInlineWithGeneric):\n   \tmodel = ...\n\nclass NavBarEntryAdmin(GenericAdminModelAdmin):\n   \tinlines = [PagesInline, ]\n\n...\n```\n\nNote that you can't mix and match.  If you're going to use a generic inline, the class using it must inherit from `GenericAdminModelAdmin`.\n\n## Specifying which fields are handled\n\nIn most cases _django-genericadmin_ will correctly figure out which fields on your model are generic foreign keys and just do the right thing. If you want to specify the fields yourself (Control your own destiny and all that) you can use the `generic_fk_fields` attribute on the admin class. Note that you can specify the fields on each admin class for inline admins. So, for the above mentioned inline admin, you would do it like so:\n\n```python\nclass PagesInline(TabularInlineWithGeneric):\n   \tmodel = AReallyCoolPage\n\tgeneric_fk_fields = [{\n        'ct_field': \u003cfield_name_for_contenttype_fk\u003e,\n        'fk_field': \u003cfield_name_for_object_id\u003e,\n    }]\n```\n\nIf you want to use more then one field pair, you can just add more dicts to the list.\n\nIf you use the `ct_field` and `ct_fk_field` attributes _django-genericadmin_ will always just ignore those fields and not even try to use them.\n\n## Blacklisting Content Types\n\nSpecific content types can be removed from the content type select list. Example:\n\n```python\nclass NavBarEntryAdmin(GenericAdminModelAdmin):\n   \tcontent_type_blacklist = ('auth/group', 'auth/user', )\n```\n\n## Whitelisting Content Types\n\nSpecific content types that can be display from the content type select list. Example:\n\n```python\nclass NavBarEntryAdmin(GenericAdminModelAdmin):\n   \tcontent_type_whitelist = ('auth/message', )\n```\n\nNote that this only happens on the client; there is no enforcement of the blacklist at the model level.\n\n## Lookup parameters by Content Type\n\nSupply extra lookup parameters per content type similar to how limit_choices_to works with raw id fields. Example:\n\n```python\nclass NavBarEntryAdmin(GenericAdminModelAdmin):\n    content_type_lookups = {'app.model': {'field': 'value'}\n```\n\n## True Polymorphic Relationships\n\n`django-genericadmin` also provides a UI to easily manage a particularly useful model that, when used as an inline on another model, enables relations from any entry of any model to any other entry of any other model. And, because it has a generic relationship moving in both directions, it means it can be attached as an inline _to any model_ without having to create unique, individual foreign keys for each model you want to use it on.\n\nHere's an example of a polymorphic model:\n\n```python\nfrom django.db import models\nfrom django.contrib.contenttypes.models import ContentType\nfrom django.contrib.contenttypes import generic\n    \nclass RelatedContent(models.Model):\n    \"\"\"\n    Relates any one entry to another entry irrespective of their individual models.\n    \"\"\"\n    content_type = models.ForeignKey(ContentType)\n    object_id = models.PositiveIntegerField()\n    content_object = generic.GenericForeignKey('content_type', 'object_id')\n\n    parent_content_type = models.ForeignKey(ContentType, related_name=\"parent_test_link\")\n    parent_object_id = models.PositiveIntegerField()\n    parent_content_object = generic.GenericForeignKey('parent_content_type', 'parent_object_id')\n\n    def __unicode__(self):\n        return \"%s: %s\" % (self.content_type.name, self.content_object)\n```\n\nAnd here's how you'd set up your admin.py:\n\n```python\nfrom whateverapp.models import RelatedContent\nfrom genericadmin.admin import GenericAdminModelAdmin, GenericTabularInline\n    \nclass RelatedContentInline(GenericTabularInline):\n    model = RelatedContent\n    ct_field = 'parent_content_type' # See below (1).\n    ct_fk_field = 'parent_object_id' # See below (1).\n        \nclass WhateverModelAdmin(GenericAdminModelAdmin): # Super important! See below (2).\n    content_type_whitelist = ('app/model', 'app2/model2' ) # Add white/black lists on this class\n    inlines = [RelatedContentInline,]\n```\n        \n(1) By default `ct_field` and `ct_fk_field` will default to `content_type` and `object_id` respectively. `ct_field` and `ct_fk_field` are used to create the parent link from the inline to the model you are attaching it to (similar to how Django does this attachment using foreign keys with more conventional inlines). You could also leave this configuration out of your inline classes but, if you do that, I encourage you to change the model attributes from `parent_content_type` \u0026 `parent_object_id` to `child_content_type` \u0026 `child_object_id`. I say this because, when it comes time to make queries, you'll want to know which direction you're 'traversing' in.\n\n(2) Make sure that whatever the admin classes are utilizing these inlines are subclasses of `GenericAdminModelAdmin` from `django-genericadmin` or else the handy-dandy javascript-utilizing interface won't work as intended.\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farthanson%2Fdjango-genericadmin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farthanson%2Fdjango-genericadmin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farthanson%2Fdjango-genericadmin/lists"}