How to get the current user in the form?

When creating an article, I list all blogs. And you need to get just a list of blogs created by the user. I create a form based on the model. Use this function when the restriction of the sample. Everything works, only problem is how to obtain data about the user which is currently logged.
def __init__(self, *args, **kwargs):
 super(BlogPostNewForms, self).__init__(*args, **kwargs)
 if self.instance:
 self.fields['blog'].queryset = MyBlog.objects.filter(author=MyProfile.objects.get(user=IMATCHSKILLS))
June 8th 19 at 17:12
2 answers
June 8th 19 at 17:14
self.request.user
self.user
In the first and in the second case, writes that the object has no such attribute.
However, the model user attribute is present:
Maybe I made a mistake somewhere in the form or view?
Code predstavleniya:
def blog_post_new(request):
 if request.method == "POST":
 form = BlogPostNewForms(request.POST or None)
 if form.is_valid():
 save_form = form.save(commit=False)
 save_form.user = request.user
save_form.save()
 joe = User.objects.get(username=request.user.username)
 task = BlogPostNew.objects.get(slug=save_form.slug)
 assign_perm('add_blogpostnew', joe, task)
 assign_perm('change_blogpostnew', joe, task)
 assign_perm('delete_blogpostnew', joe, task)
save_form.save()
 return redirect('blog_post_detail', slug=save_form.slug)
else:
 form = BlogPostNewForms()# For Traceback error in this place
 return render(request, 'pages/blog_post_edit.html', {'form': form})

Form code in full:

class BlogPostNewForms(ModelForm):
 class Meta:
 model = BlogPostNew
 fields = '__all__'
 exclude =['user','related_posts','featured_image', 'publish_date', 'short_description','slug','short_url','_meta_title','expiry_date','gen_description','in_sitemap','keywords','categories', 'description']
 def __init__(self, *args, **kwargs):
 super(BlogPostNewForms, self).__init__(*args, **kwargs)
 if self.instance:
 # self.fields['blog'].queryset = MyBlog.objects.filter(name='myblog')
 self.fields['blog'].queryset = MyBlog.objects.filter(author=self.user) # And then the same error
- magnus_Mertz11 commented on June 8th 19 at 17:17
version Jangi
the model is implemented as a user? - marilyne_Roh commented on June 8th 19 at 17:20
Django 1.10.7
The user model is implemented in a communication one-to-one with the standard User model.
Model Profile
Model Profile
class MyProfile(models.Model):
 user = models.OneToOneField("auth.User")
 date_of_birth = models.DateField(null=True)
 bio = models.TextField()
 avatar = models.ImageField(upload_to='uploads/', default='uploads\gallery\Berlin, Germany.jpg')

 def __str__(self):
 return self.user.username

Such a connection is used as recommended when using the application mezannine. - magnus_Mertz11 commented on June 8th 19 at 17:23
June 8th 19 at 17:16
In the init of the form before *args add another argument user_id=None
In the view when you initialize the form pass back the request.user.id
In the Inita check - if there is a user_id to filter a queryset

Find more questions by tags DjangoPython