Skip to content

Instantly share code, notes, and snippets.

@mfcovington
Created July 16, 2015 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfcovington/c4249bcfde8c1a12cb0c to your computer and use it in GitHub Desktop.
Save mfcovington/c4249bcfde8c1a12cb0c to your computer and use it in GitHub Desktop.
###############
# settings.py #
###############
RELATIONSHIPS = [
('alerts', 'insurers'),
('alerts', 'producers'),
('alerts', 'vendors'),
]
#############
# models.py #
#############
from django.conf import settings
from django.db import models
def relations_base(object_type):
if 'django_velcro' in settings.INSTALLED_APPS:
from django_velcro.utils import relations_abstract_base
RelationsBase = relations_abstract_base(object_type)
else:
RelationsBase = models.Model
return RelationsBase
class Alert(relations_base('alerts')):
...
class Insurer(relations_base('insurers')):
...
class Producer(relations_base('producers')):
...
class Vendor(relations_base('vendors')):
...
############
# admin.py #
############
from django.conf import settings
from django.contrib import admin
from .models import Alert, Insurer, Producer, Vendor
def admin_base(object_type):
if 'django_velcro' in settings.INSTALLED_APPS:
from django_velcro.utils import generic_admin_base
GenericAdminBase = generic_admin_base(object_type)
else:
GenericAdminBase = admin.ModelAdmin
return GenericAdminBase
@admin.register(Alert)
class AlertAdmin(admin_base('alerts')):
pass
@admin.register(Insurer)
class InsurerAdmin(admin_base('insurers')):
pass
@admin.register(Producer)
class ProducerAdmin(admin_base('producers')):
pass
@admin.register(Vendor)
class VendorAdmin(admin_base('vendors')):
pass
#############
# models.py #
#############
from django.conf import settings
from django.db import models
from django_velcro import (AlertsInsurersRelationship,
AlertsProducersRelationship, AlertsVendorsRelationship)
class Alert(models.Model):
related_insurers = GenericRelation(AlertsInsurersRelationship,
content_type_field='insurers_content_type',
object_id_field='insurers_object_id',
related_query_name='alerts',
)
related_producers = GenericRelation(AlertsProducersRelationship,
content_type_field='producers_content_type',
object_id_field='producers_object_id',
related_query_name='alerts',
)
related_vendors = GenericRelation(AlertsVendorsRelationship,
content_type_field='vendors_content_type',
object_id_field='vendors_object_id',
related_query_name='alerts',
)
class Insurer(models.Model):
related_alerts = GenericRelation(AlertsInsurersRelationship,
content_type_field='alerts_content_type',
object_id_field='alerts_object_id',
related_query_name='insurers',
)
class Producer(models.Model):
related_alerts = GenericRelation(AlertsProducersRelationship,
content_type_field='alerts_content_type',
object_id_field='alerts_object_id',
related_query_name='producers',
)
class Vendor(models.Model):
related_alerts = GenericRelation(AlertsVendorsRelationship,
content_type_field='alerts_content_type',
object_id_field='alerts_object_id',
related_query_name='vendors',
)
############
# admin.py #
############
from django.conf import settings
from django.contrib import admin
from django_velcro.admin import (AlertsToInsurersAdminBase,
AlertsToProducersAdminBase, AlertsToVendorsAdminBase,
InsurersToAlertsAdminBase, ProducersToAlertsAdminBase,
VendorsToAlertsAdminBase
from genericadmin.admin import GenericAdminModelAdmin
from .models import Alert, Insurer, Producer, Vendor
@admin.register(Alert)
class AlertAdmin(GenericAdminModelAdmin):
inlines = [
AlertsToInsurersAdminBase,
AlertsToProducersAdminBase,
AlertsToVendorsAdminBase,
]
@admin.register(Insurer)
class InsurerAdmin(GenericAdminModelAdmin):
inlines = [InsurersToAlertsAdminBase]
@admin.register(Producer)
class ProducerAdmin(GenericAdminModelAdmin):
inlines = [ProducersToAlertsAdminBase]
@admin.register(Vendor)
class VendorAdmin(GenericAdminModelAdmin):
inlines = [VendorsToAlertsAdminBase]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment