python - ManyToManyField with through on an abstract model -
python - ManyToManyField with through on an abstract model -
got interesting 1 here.. i've shortened models create easier 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 participant journey, journey abstract because can made number of different methods of transport each of have own respective fields. think setup right i'm getting next error message:
error: 1 or more models did not validate: kandersteg.planejourney: 'participants' manually-defined m2m relation through model participantjourney, not have foreign keys participant , planejourney
i need maintain manual definition of link table can link payment said journey don't know go next this, if shed lite greatful!
cheers, alex
django not consider generic foreign keys required foreign keys in through model, , there no way define manytomany relationship through in abstract base of operations class.
however, there feature request in django (ticket #11760), enables utilize %(class)s in through field, e.g. using through='pariticipant_%(class)s' in journey model, , must create throught table manually every kid of journey. said, open feature request yet.
python django many-to-many abstract django-orm
Comments
Post a Comment