[Python] django model foreign key to one in a list of models

Discussão em 'Python' iniciado por Stack, Maio 31, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Profile class which I like to use for my CustomUser and Shipper classes. Is there a way to tell django that models.ForeignKey(to=[CustomUser, Shipper]) in Profile model so that my profile would have a foreign key to EITHER CustomUser OR Shipper models?

    I tried putting models.ForeignKey(to=[Profile]) in both CustomUser and Shipper classes but the problem is that when I try to run py manage.py createsuperuser I face and error saying I violate NOT NULL rule and I must set a profile for my superuser.
    I also tried to put Shipper and CustomUser into an ABSTRACT parent class and set this relation of models.ForeignKey(to=[Profile]) in parent but again the problem is that django does not allow to have foregin key to abstract classes
    I do can put a foreign key relationship in both Shipper and CustomUser models but I think this is not the best practise.

    What should I do?

    Continue reading...

Compartilhe esta Página