ManyToManyField using an abstract model

We have an interesting interest. I shortened the models to make it easier to understand.

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 participant-participant model associates the participant with the journey, now the journey is abstract, because it can be performed by any number of different modes of transport, each of which will have its own corresponding fields. I think this setting is correct, but I get the following error message:

Error: one or more models were not confirmed: kandersteg.planejourney: "Participants" is the manually related m2m relationship through the "Participant" model, which does not have foreign keys to the Participant and PlaneJourney

I need to save the manual definition of the link table so that I can also associate the payment with the specified trip, so I really don’t know where to go next with this, if someone can shed some light, I would be really nice!

Cheers, Alex

+4
source share
2 answers

Django does not consider shared foreign keys as required foreign keys in a pass-through model, and there is currently no way to define ManyToMany relationships with pass-throughs in an abstract base class.

However, in django there is a function request ( ticket # 11760 ) that also allows you to use %(class)s in the field, for example using the through='Pariticipant_%(class)s' model in Journey , and then you must manually create the pass-through table for each child of Journey . But, as I said, this is just an open function request.

+1
source

You can try to define Journey as non-abstract, but then you will get data divided into several tables.

I found something interesting here: fooobar.com/questions/1392196 / ...

0
source

Source: https://habr.com/ru/post/1392194/


All Articles