how can i get current username in model field ??

24 views
Skip to first unread message

omar ahmed

unread,
Apr 1, 2019, 9:31:28 AM4/1/19
to Django users
i want to put value of logged in user.username in author field
class Comment(models.Model):
leaguenews = models.ForeignKey(LeagueNews, on_delete= models.CASCADE, related_name='comments')
author = models.CharField(max_length=200)
text = models.TextField()

Joel Mathew

unread,
Apr 1, 2019, 9:39:25 AM4/1/19
to django...@googlegroups.com
This does not make sense to me. A model is not a dynamic entity. It is something that's created beforehand. Getting the current username into model should be done in your logic (views), or the template
Sincerely yours,

 Joel G Mathew



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8a24a020-af3a-4a5f-ac25-5da3aa1fa8db%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

omar ahmed

unread,
Apr 1, 2019, 9:55:12 AM4/1/19
to Django users
ok i understood ....
does it work :
request.user.username


On Monday, April 1, 2019 at 3:39:25 PM UTC+2, Joel Mathew wrote:
This does not make sense to me. A model is not a dynamic entity. It is something that's created beforehand. Getting the current username into model should be done in your logic (views), or the template
Sincerely yours,

 Joel G Mathew



On Mon, 1 Apr 2019 at 19:01, omar ahmed <omark...@gmail.com> wrote:
i want to put value of logged in user.username in author field
class Comment(models.Model):
leaguenews = models.ForeignKey(LeagueNews, on_delete= models.CASCADE, related_name='comments')
author = models.CharField(max_length=200)
text = models.TextField()

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Test Bot

unread,
Apr 1, 2019, 9:57:48 AM4/1/19
to django...@googlegroups.com
+1
DoesNotMakesSenseError("Author of any comment should not be related to the currently logged in user")

omar ahmed

unread,
Apr 1, 2019, 12:02:21 PM4/1/19
to Django users
explain please


On Monday, April 1, 2019 at 3:57:48 PM UTC+2, OnlineJudge95 wrote:
+1
DoesNotMakesSenseError("Author of any comment should not be related to the currently logged in user")

On Mon, Apr 1, 2019, 7:09 PM Joel Mathew <jo...@joel.su> wrote:
This does not make sense to me. A model is not a dynamic entity. It is something that's created beforehand. Getting the current username into model should be done in your logic (views), or the template
Sincerely yours,

 Joel G Mathew



On Mon, 1 Apr 2019 at 19:01, omar ahmed <omark...@gmail.com> wrote:
i want to put value of logged in user.username in author field
class Comment(models.Model):
leaguenews = models.ForeignKey(LeagueNews, on_delete= models.CASCADE, related_name='comments')
author = models.CharField(max_length=200)
text = models.TextField()

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

omar ahmed

unread,
Apr 1, 2019, 2:04:06 PM4/1/19
to Django users
ok i solved it
name = request.user.username
form = CommentForm(initial={'author': name})
return render(request, 'core/add_comment_to_news.html', {'form':form

Ing.Daniel Bojorge

unread,
Apr 1, 2019, 4:45:55 PM4/1/19
to django...@googlegroups.com
Here I let you how I do.


 
Mi Blog
Nicaragua

"Si ustedes permanecen unidos a mí, y si permanecen fieles a mis enseñanzas, pidan lo que quieran y se les dará.
(Juan 15:7 DHH)
Bendito el varón que se fía en el SEÑOR, y cuya confianza es el SEÑOR.
(Jeremías 17:7 RV2000)



To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Guru Murthy

unread,
Apr 1, 2019, 9:23:02 PM4/1/19
to django...@googlegroups.com
Hi Omar Ahmed,
  You cannot give the user model value to the field that you can do in views.py and forms.py. visit more about how to create forms in django.

Reply all
Reply to author
Forward
0 new messages