Home > OS >  Not able to get current logged in user in django model
Not able to get current logged in user in django model

Time:08-30

I am trying to get current logged in user through my model so that I can only see the current user in my order page dropdown:

  1. I have gone through a lot of documents which state that it is not that easy or feasible to get current logged in user in model.

  2. I have tried other method like getting AUTH_USER_MODEL but it is returning admin level users as well so not solving the problem.

  3. I am also sending the current logged in user from my views file but dont know how to access it inside form class, able to access it in init but dont know how it can be accessed in class.

models.py :

from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.auth import get_user_model
from django.http import HttpResponse,HttpRequest

class Customer(models.Model):
    name = models.CharField(max_length=200,null=True)
    user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
    date_created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

class Order(models.Model):
    product = models.ForeignKey(Product,null=True, on_delete=models.SET_NULL)
    #customer = models.ForeignKey(settings.AUTH_USER_MODEL,null=True, on_delete=models.SET_NULL)
    customer = models.ForeignKey(Customer,null=True, on_delete=models.SET_NULL)
    date_ordered = models.DateTimeField(auto_now_add=True)
    status = models.CharField(max_length=20,default= 'PENDING')
    def __str__(self):
        return str(self.customer.id)

forms.py :

class createorderform(ModelForm):
    def __init__(self,*args,**kwargs):
        self._instance=kwargs.pop('instance',None)
        super().__init__(*args,**kwargs)

    #def __init__(self,instance):
    #    self.user = instance
    #    super().__init__(instance)

    class Meta:
        model=Order
        fields="__all__"
        exclude=['status']

Views.py

def placeorder(request,i):
    try:
        products = Product.objects.all()
        customer = Customer.objects.get(id=i)
        print("Customer:",customer)
        #form=createorderform(prod=products,cust=customer)
        form=createorderform(instance=customer)
        #form=createorderform()
        if(request.method=='POST'):
            form=createorderform(request.POST,instance=customer)
            if(form.is_valid()):
                form.save()
                return redirect('/')
        context={'form':form}
        return render(request,'app_name/placeorder.html',context)
    except:
        print("Error occurred : {exec}".format(exec=traceback.format_exc()))

What I am getting is all the users:

enter image description here

What I want is to only show the current user in drop down.

Please help or guide me in the right direction.

Thanks in advance!!

CodePudding user response:

Something like

from django.forms import ModelForm
from django.views.generic import CreateView


class CreateOrderForm(ModelForm):
    class Meta:
        model = Order
        exclude = ['status', 'customer']

    def __init__(self, **kwargs):
        self.customer = kwargs.pop('customer')
        super().__init__(**kwargs)

    def save(self, commit=True):
        self.instance.customer = self.customer
        return super().save(commit=commit)


class PlaceOrderView(CreateView):
    model = Order
    form_class = CreateOrderForm
    template_name = 'app_name/placeorder.html'

    def get_form_kwargs(self):
        return {
            **super().get_form_kwargs(),
            'customer': Customer.objects.get(user=self.request.user),
        }

should be enough.

CodePudding user response:

So I have kind of tried one solution and this one work, although I have tried it earlier but in between 100's of documents and solutions the easy one got lost :

In my forms.py :

class createorderform(ModelForm):
    def __init__(self,*args,**kwargs):
        self._instance=kwargs.pop('instance',None)
        super().__init__(*args,**kwargs)

    #def __init__(self,instance):
    #    self.user = instance
    #    super().__init__(instance)

    class Meta:
        model=Order
        fields="__all__"
        exclude=['customer','status']

I excluded the customer field and instead populated it in my views. py:


def placeorder(request,i):
    try:
        products = Product.objects.all()
        customer = Customer.objects.get(id=i)
        print("Customer:",customer)
        #form=createorderform(prod=products,cust=customer)
        form=createorderform(instance=customer)
        #form=createorderform()
        if(request.method=='POST'):
            form=createorderform(request.POST,instance=customer)
            if(form.is_valid()):
                curr_user = form.save(commit=False)
                curr_user.customer = customer
                curr_user.save()
                return redirect('/')
        context={'form':form}
        return render(request,'app_name/placeorder.html',context)
    except:
        print("Error occurred : {exec}".format(exec=traceback.format_exc()))

So I am overriding the save() and building my current logged in user in the view itself while saving it. It's storing the same user in db as well.

EDIT:

def placeorder(request,i):
    try:
        products = Product.objects.all()
        customer = Customer.objects.get(id=i)
        print("Customer:",customer)
        #form=createorderform(prod=products,cust=customer)
        form=createorderform()
        #form=createorderform(instance=customer)
        if(request.method=='POST'):
            form=createorderform(request.POST)
            #form=createorderform(request.POST,instance=customer)
            if(form.is_valid()):
                curr_user = form.save(commit=False)
                curr_user.customer = customer
                curr_user.save()
                return redirect('/')
        context={'form':form}
        return render(request,'app_name/placeorder.html',context)
    except:
        print("Error occurred : {exec}".format(exec=traceback.format_exc()))

So I have change my form object and I am not sending my current logged in user to forms any more so just creating the object without "instance=customer" which got my current logged in user.

Now we don't need to handle the instance in init method of createorderform form :

class createorderform(ModelForm):
    #def __init__(self,*args,**kwargs):
    #    self._instance=kwargs.pop('instance',None)
    #    super().__init__(*args,**kwargs)

    #def __init__(self,instance):
    #    self.user = instance
    #    super().__init__(instance)

    class Meta:
        model=Order
        fields="__all__"
        exclude=['customer','status']

  • Related