Created
July 16, 2015 03:25
-
-
Save mfcovington/c4249bcfde8c1a12cb0c to your computer and use it in GitHub Desktop.
django-velcro example for https://www.reddit.com/r/django/comments/3ddvo7/lets_talk_genericforeignkeys/
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
############### | |
# 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 | |
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
############# | |
# 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