Djangoでウェブアプリケーションの実装練習をしているのですが、
CreateViewで複数のmodelを利用する方法が分かりません。
具体的には、models.pyでRoomModelとChatModelというものを設けており、RoomModelのpkをChatModelの作成時に利用したいのです。
方法をご存知の方がいらっしゃれば、ご回答頂けると幸いです。
・models.py
Python
1POSITION = (('primary', '1'), ('success', '2'), ('danger', '3'), ('warning', '4'), ('dark', '5')) 2class ChatModel(models.Model): 3 room_id = models.IntegerField(default=0) 4 content = models.TextField() 5 author = models.CharField(max_length=100) 6 priority = models.CharField(max_length=50, choices = POSITION) 7 duedate = models.DateField(auto_now_add=True) 8 9class RoomModel(models.Model): 10 title = models.CharField(max_length=50) 11 author = models.CharField(max_length=100) 12 position_1 = models.CharField(max_length=30, default='賛成', blank=True) 13 position_2 = models.CharField(max_length=30, default='反対', blank=True) 14 position_3 = models.CharField(max_length=30, default='', blank=True) 15 position_4 = models.CharField(max_length=30, default='', blank=True) 16 position_5 = models.CharField(max_length=30, default='', blank=True) 17 duedate = models.DateField(auto_now_add=True)
・views.py
Python
1## ルーム作成 2class RoomCreate(CreateView): 3 template_name = 'room_create.html' 4 model = RoomModel 5 fields = {'title', 'author', 'position_1', 'position_2', 'position_3', 'position_4', 'position_5'} 6 success_url = reverse_lazy('home') 7 8 9## 発言作成 10class ChatCreate(CreateView): 11 template_name = 'chat_create.html' 12 model = ChatModel 13 fields = {'room_id', 'content', 'author', 'priority'} 14 success_url = reverse_lazy('home')
初学者ゆえ、足りない情報もあると思います。
ご指摘いただければ、随時追記させて頂きたいと思いますので、よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。