1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- from django.contrib.contenttypes.fields import GenericForeignKey
- from django.contrib.contenttypes.models import ContentType
- from rapid.rapidforms import RapidAlternativesField
- from django.db import models
- __author__ = 'marcos'
- """
- Model fields specific for Rapid Django.
- """
- class AlternativeData(GenericForeignKey):
- """
- A generic relation that Rapid will display as an inline form or a group of data at the
- edit and view actions.
- Create like a Django GenericForeignKey, but use an AlternativeDataTables instead of a
- ForeignKey to ContentTypes.
- """
- def formfield(self, **kwargs):
- kwargs['form_class'] = RapidAlternativesField
- return super(GenericForeignKey, self).formfield(**kwargs)
- class AlternativeDataTables(models.ForeignKey):
- """
- Use an AlternativeDataTables model field on AlternativeData represetntations, instead of
- a ForeignKey with ContentTypes.
- """
- def __init__(self, alternatives=[], **kwargs):
- """
- Receives the same named parameters of a ForeignKey, but instead of a target table,
- receives a list of the tables that may keep the alternative data.
- :param alternatives: List of the tables that hold the alternative data.
- :param kwargs: Arguments to the ForeignKey, without a relationship target.
- """
- pks=[]
- try:
- pks = [ContentType.objects.get_for_model(a).pk for a in alternatives]
- except RuntimeError:
- #Querying ContentType within a content type is an issue at migrations
- pass
- kwargs['limit_choices_to'] = {'pk__in': pks}
- kwargs['to'] = ContentType
- super(AlternativeDataTables, self).__init__(**kwargs)
- self.alternatives = alternatives
|