Created
June 25, 2015 14:15
-
-
Save beratdogan/bd57ef57e53b12b48d9e to your computer and use it in GitHub Desktop.
[Django] Changing field widget when when using inlines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.contrib import admin | |
from django.contrib.sites.models import Site | |
from django.contrib.sites.admin import SiteAdmin | |
from django.utils.translation import ugettext_lazy as _ | |
from colorful.fields import RGBColorField # third-party app | |
from .models import SiteOption | |
OPTION_COLOR_VARIABLE_CHOICES = ( | |
('header_color', _('Header Color')), | |
# ... | |
) | |
OPTION_VARIABLE_CHOICES = ( | |
('contact_email', _(u'[email protected]')), | |
# ... | |
) | |
# Register site option model as inline into sites | |
# by unregistering Site model first | |
class ThemeColorsInlineAdmin(admin.TabularInline): | |
model = SiteOption | |
verbose_name = _('Theme Color') | |
verbose_name_plural = _('Theme Colors') | |
extra = 0 | |
def formfield_for_dbfield(self, db_field, **kwargs): | |
if db_field.attname == 'variable': | |
# Change variable field's widget with selectbox! | |
kwargs['widget'] = forms.Select( | |
choices=OPTION_COLOR_VARIABLE_CHOICES) | |
if db_field.attname == 'value': | |
# change value field's widget with color picker! | |
db_field = RGBColorField() | |
return super(ThemeColorsInlineAdmin, self).formfield_for_dbfield( | |
db_field, **kwargs) | |
def get_queryset(self, request): | |
""" | |
Limit result to get only color variables. | |
""" | |
qs = super(ThemeColorsInlineAdmin, self).get_queryset(request) | |
return qs.filter(variable__in=[f[0] for f in OPTION_COLOR_VARIABLE_CHOICES]) | |
class SiteOptionInlineAdmin(admin.TabularInline): | |
model = SiteOption | |
extra = 0 | |
def formfield_for_dbfield(self, db_field, **kwargs): | |
if db_field.attname == 'variable': | |
# Change variable field's widget with selectbox! | |
kwargs['widget'] = forms.Select(choices=OPTION_VARIABLE_CHOICES) | |
return super(SiteOptionInlineAdmin, self).formfield_for_dbfield(db_field, **kwargs) | |
def get_queryset(self, request): | |
""" | |
Limit result to get only non-color variables. | |
""" | |
qs = super(SiteOptionInlineAdmin, self).get_queryset(request) | |
return qs.exclude(variable__in=[f[0] for f in OPTION_COLOR_VARIABLE_CHOICES]) | |
class MySiteAdmin(SiteAdmin): | |
inlines = [ThemeColorsInlineAdmin, SiteOptionInlineAdmin] | |
admin.site.unregister(Site) | |
admin.site.register(Site, MySiteAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from django.contrib.sites.models import Site | |
from django.contrib.sites.managers import CurrentSiteManager | |
class SiteOption(models.Model): | |
variable = models.CharField(max_length=80) | |
value = models.TextField() | |
site = models.ForeignKey(Site) | |
objects = models.Manager() | |
on_site = CurrentSiteManager() | |
class Meta: | |
unique_together = (('variable', 'site'),) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment