Metadata-Version: 2.2
Name: admin_pretty_gfk
Version: 0.0.3
Summary: Custom Django admin mixin and widgets for handling generic foreign keys (GFKs) more intuitively.
Author-email: Andrii Tierzov <avtierzov@gmail.com>
License: MIT License
        
        Copyright (c) 2025 Andrii Tierzov
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
        
Project-URL: Homepage, https://github.com/DeDuHaNcHiK/admin-pretty-gfk
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Django>=4.2.18

# admin_pretty_gfk

Custom Django admin mixin and widgets for handling generic foreign keys (GFKs) more intuitively.

## Features
- **PrettyGFKModelAdminMixin**: A mixin that enhances Django's admin interface for models with GFKs.
- **ForeignKeyContentIdWidget**: A raw ID widget with a fallback URL to prevent errors.
- **ContentTypeSelect**: A dropdown widget that dynamically updates related object selection and resets the foreign key field when the content type changes.

## Installation
```sh
pip install admin_pretty_gfk
```

## Usage
### Import and Use in ModelAdmin
#### Using the Mixin
```python
from admin_pretty_gfk.mixins import PrettyGFKModelAdminMixin
from django.contrib import admin

class AdvertAdmin(PrettyGFKModelAdminMixin, admin.ModelAdmin):
    pass
```

#### Using the Widget
```python
from admin_pretty_gfk.widgets import ContentTypeSelect, ForeignKeyContentIdWidget
from django import forms
from django.contrib import admin
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import ManyToOneRel

from common.models import Buy, Sell, Rent, Advert

class AdvertModelForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(AdvertModelForm, self).__init__(*args, **kwargs)
        try:
            model = self.instance.content_type.model_class()
            model_key = model._meta.pk.name
        except (AttributeError, ObjectDoesNotExist):
            model = self.fields['content_type'].queryset[0].model_class()
            model_key = 'id'
        self.fields['object_id'].widget = ForeignKeyContentIdWidget(
            rel=ManyToOneRel(model_key, model, 'id'),
            admin_site=admin.site
        )

    class Meta:
        model = Advert
        fields = "__all__"
        widgets = {
            'content_type': ContentTypeSelect
        }

class AdvertAdmin(admin.ModelAdmin):
    form = AdvertModelForm
```

#### Registering Models
```python
admin.site.register(Buy)
admin.site.register(Sell)
admin.site.register(Rent)
admin.site.register(Advert, AdvertAdmin)
```

## How It Works
1. `PrettyGFKModelAdminMixin` automates form handling for models with GFKs.
2. `ContentTypeSelect` updates object selection dynamically when the content type changes.
3. `ForeignKeyContentIdWidget` ensures proper URL handling for related objects.

## Screenshots
Default create form
![default create form](https://github.com/DeDuHaNcHiK/admin-pretty-gfk/blob/main/images/before_create.png)
Create form with admin_pretty_gfk
![create form with admin_pretty_gfk](https://github.com/DeDuHaNcHiK/admin-pretty-gfk/blob/main/images/after_create.png)
Default edit object form
![default edit object form](https://github.com/DeDuHaNcHiK/admin-pretty-gfk/blob/main/images/before_change.png)
Edit object form with admin_pretty_gfk
![Edit object form](https://github.com/DeDuHaNcHiK/admin-pretty-gfk/blob/main/images/after_change.png)

## License
This project is licensed under the MIT License.

