У меня есть поле с именем 'rep' в моей форме, я хочу получить представление, запросить пользователя, равного 'rep', передать его в мое представление и сохранить его в получателе в модели:
Я пробовал следующую строку: form.instance.recipient = Message.objects.get (receient = self.request.rep)
но я получаю
'WSGIRequest' object has no attribute 'rep'
Просмотр:
view.py
класс compose (CreateView):
model = Message
form_class=ComposeForm
template_name='django_messages/compose.html'
def form_valid(self,form):
# form.instance.recipient = Message.objects.get(recipient=self.request.rep)
form.instance.sender =self.request.user
return super(compose,self).form_valid(form)
form.py
class ComposeForm(forms.ModelForm):
"""
A simple default form for private messages.
"""
class Meta:
model = Message
# fields ='__all__'
fields=['rep','subject','body',]
#recipient = CommaSeparatedUserField(label=_(u"Recipient"))
# subject = forms.CharField(label=_(u"Subject"), max_length=140)
# subject='hello'
body = forms.CharField(label=_(u"Body"),
widget=forms.Textarea(attrs={'rows': '12', 'cols':'55'}))
rep=forms.CharField()
model.py
class Message(models.Model):
"""
A private message from user to user
"""
subject = models.CharField(_("Subject"), max_length=140)
body = models.TextField(_("Body"))
sender = models.ForeignKey(AUTH_USER_MODEL, related_name='sent_messages', verbose_name=_("Sender"), on_delete=models.PROTECT)
recipient = models.ForeignKey(AUTH_USER_MODEL, related_name='received_messages', null=True, blank=True, verbose_name=_("Recipient"), on_delete=models.SET_NULL)
parent_msg = models.ForeignKey('self', related_name='next_messages', null=True, blank=True, verbose_name=_("Parent message"), on_delete=models.SET_NULL)
sent_at = models.DateTimeField(_("sent at"), null=True, blank=True)
read_at = models.DateTimeField(_("read at"), null=True, blank=True)
replied_at = models.DateTimeField(_("replied at"), null=True, blank=True)
sender_deleted_at = models.DateTimeField(_("Sender deleted at"), null=True, blank=True)
recipient_deleted_at = models.DateTimeField(_("Recipient deleted at"), null=True, blank=True)
введите описание изображения здесь