3
我有模型消息,它是表單管理器。要填充「用戶」和「組」字段,我需要知道當前記錄的用戶標識,但我不知道如何在保存前獲取它。如何在Django管理面板中獲取當前記錄的用戶ID?
class Message(models.Model):
title = models.CharField(max_length = 100)
text = models.TextField()
date = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, null = True, blank = True)
main_category = models.ForeignKey(MainCategory)
sub_category = models.ForeignKey(SubCategory)
groups = models.ManyToManyField(Group)
class MessageAdminForm(forms.ModelForm):
def __init__(self, *arg, **kwargs):
super(MessageAdminForm, self).__init__(*arg, **kwargs)
self.initial['main_category'] = MainCategory.objects.get(title = 'News')
錯誤---「超級」對象有沒有屬性「保存」 要解決使用--- 超(MessageAdmin,個體經營).save_model (請求,obj,表單,更改) – GrvTyagi