Got an interesting one here.. I've shortened the models to make it easier to comprehend..
class Participant(Person):
passport_number = models.IntegerField(verbose_name=_('Passport Number'), db_column=u'PassportNumber')
class Meta:
db_table = u'Participant'
class Journey(BaseModel):
participants = models.ManyToManyField(Participant, related_name='%(app_label)s_%(class)s_participants', through=u'ParticipantJourney')
class Meta:
abstract = True
class PlaneJourney(Journey):
flight_number = models.CharField(max_length=16, verbose_name=_('Flight Number'), db_column=u'FlightNumber')
class Meta:
db_table = u'PlaneJourney'
class ParticipantJourney(BaseModel):
participant = models.ForeignKey(Participant, verbose_name=_('Participant'), db_column=u'ParticipantId')
journey_content_type = models.ForeignKey(ContentType, related_name='journey_content_type')
journey_object_id = models.PositiveIntegerField()
journey = generic.GenericForeignKey('journey_content_type', 'journey_object_id') # models.ForeignKey(Journey, verbose_name=_('Journey'), db_column=u'JourneyId')
payment_content_type = models.ForeignKey(ContentType, related_name='payment_content_type')
payment_object_id = models.PositiveIntegerField()
payment = generic.GenericForeignKey('payment_content_type', 'payment_object_id') # models.ForeignKey(Payment, verbose_name=_('Payment'), db_column=u'PaymentId')
class Meta:
db_table = u'ParticipantJourney'
The ParticipantJourney model links a participant to a journey, now a journey is abstract because it can be made by any number of different methods of transport each of which will have their own respective fields. I think this setup is correct but I'm getting the following error message:
Error: One or more models did not validate: kandersteg.planejourney: 'participants' is a manually-defined m2m relation through model ParticipantJourney, which does not have foreign keys to Participant and PlaneJourney
I need to keep the manual definition of the link table so I can also link a payment to said journey so I don't really know where to go next with this, if anyone could shed some light I would be really greatful!
Cheers, Alex
Django does not consider generic foreign keys as the required foreign keys in through model, and currently there is no way to define a ManyToMany relationship with through in an abstract base class.
However, there is a feature request in django (ticket #11760), that enables you to also use %(class)s
in through field, e.g. by using through='Pariticipant_%(class)s'
in Journey
model, and then you must create the throught table manually for every child of Journey
. But as I said, its just an open feature request yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With