1



I would like to pass the user instance of the current user to a variable in a class-based view and feed it in a second step, within a python script, to a database table column defined as author = models.ForeignKey(User, on_delete=models.PROTECT). Related to that column I get the following integrity error: NOT NULL constraint failed.. From the traceback I can see that my variable loggedUser, which I had attributed self.request.user, is a SimpleLazyObject.

I have found another question similar to mine. But it discusses the issue related to functions - I am having difficulties to adapt the answers to my case of a class-based view.

How can I pass my user instance?
Thank you!

models.py

from django.db import models
from django.contrib.auth.models import User

class Data(models.Model):
    temperature = models.FloatField(max_length=5)
    author = models.ForeignKey(User, on_delete=models.PROTECT) 


views.py

from . import apythonscript
from django.views.generic import TemplateView

class PlotGraphView(TemplateView):

    def get_context_data(self, **kwargs):
        loggedUser = self.request.user
        context['plot'] = apythonscript.putDataInDB(loggedUser)
        return context


apythonscript.py

from app.models import Data

def putDataInDB(loggedUser):
    mydata=json.loads(response)

    for line in mydata['feeds']:
    #    try:
        data_handle = Data(temperature=line['field1'], author=loggedUser,)
        data_handle.save()
    #    except:
    #        continue

traceback

link to traceback

Anjo
  • 145
  • 1
  • 11
  • 1
    Possible duplicate of [request.user returns a SimpleLazyObject, how do I "wake" it?](https://stackoverflow.com/questions/11314905/request-user-returns-a-simplelazyobject-how-do-i-wake-it) – ascripter Mar 07 '18 at 21:30
  • Your question isn't clear to me - you aren't using your `Data` model anywhere. Please include the full traceback as well. – Alasdair Mar 07 '18 at 22:57
  • Thank you for your feedback. I adapted the example. – Anjo Mar 08 '18 at 20:51
  • You still haven’t shown the traceback. – Alasdair Mar 08 '18 at 20:53
  • My bad. It is added now. Thanks! – Anjo Mar 08 '18 at 21:24
  • Your traceback says the missing field is `climatelog_app_data.relative_humidity`, not the user. – Alasdair Mar 08 '18 at 21:45
  • Oh man! You are right. The error appeared when I was passing 'self.request.user' through a variable to the python script, but what I actually messed up at that moment was commenting out 'try and except' and then not reading carefully the traceback. – Anjo Mar 09 '18 at 20:23

1 Answers1

1

to get the currently logged in user in Django, you can simply use, request.user. In CBV, request is a class object, so you should use self.request to access request data.

example

class PlotGraphView(LoginRequiredMixin, TemplateView):
    # your stuff

    def get_current_user(self):  # this is just a custom function, you don't 
        return self.request.user


So, anywhere in the CBV's scope, you can access the current user by self.request.user

JPG
  • 82,442
  • 19
  • 127
  • 206