Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

python - Django field with multiple model option

I have a scenario where I have a model "Thread". There are two fields in it. "Sender" and "receiver". Also there are other models like "Teacher", "Student", "Principal" etc. Now in the "Thread" model, the sender can be a "Student", and receiver can be a "Teacher" OR Vice Versa. What is the solution for that:

class Thread(models.Model):
    sender = models.(???) # can't be a student relation because a sender can be a teacher too
    receiver = models.(???) # same here.

Is there any way that I can dynamically first select the model and then pick an object of that list. I have seen this functionality in Odoo. But is there anything in Django?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use Inheritance for in your Model and map your foreign key to User, and then pass either a teacher of student object.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...