简体   繁体   中英

How can I only print the the current logged in user data in Django?

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

models.py

class placement(models.Model):
    user=models.ForeignKey(User,related_name='placeid', null=True, default=None,on_delete=models.CASCADE)
    name=models.CharField(max_length=150, blank=True, null=True)  
    ad_space=models.CharField(max_length=100, blank=False, null=False)  
    PID_TYPE = (  
        ('FN','FORMAT_NATIVE'),  
        ('FNB','FORMAT_NATIVE_BANNER'),  
        ('FI','FORMAT_INTERSTITIAL'),  
        ('FB','FORMAT_BANNER'),  
        ('FMR','FORMAT_MEDIUM,RECT'),  
        ('FRV','FORMAT_REWARDED_VIDEO'),  
    )

    format = models.CharField(max_length=3,choices = PID_TYPE,default = 'FN',blank=False, null=False)
    pid=models.CharField( max_length=50,default='',blank=False, null=False)
    cpm=models.IntegerField(default=0,blank=False, null=False)
    ADS_TYPE=(
        ('FB','FACEBOOK'),
        ('G','GOOGLE'),
    )
source=models.CharField(max_length=2,choices=ADS_TYPE,default='FB',blank=False, null=False)
    comments=models.TextField(default='',blank=False, null=False)  
    objects=models.Manager()


    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("dashapp:disp")

views.py

class createPlace(LoginRequiredMixin,CreateView):  
    fields=('name','ad_space','format','pid','cpm','source','comments')
    model=placement  
    template_name='createpl.html'  
    def form_valid(self, form):  
        form.instance.user = User.objects.get(id = self.request.user.id)  
        return super(createPlace, self).form_valid(form)  


class Idlist(LoginRequiredMixin,ListView):

    model=placement
    template_name='placement_list.html'
    def get_queryset(self):
        query_set=super().get_queryset()
        return query_set.filter(placement.objects.filter(user=self.request.user))

Now What query i have to make in Idlist class in Views.py to fetch only the current logged in user related data. And the query that I'm Perfroming in Idlist is giving me error as

Exception Type: ValueError Exception Value: too many values to unpack (expected 2)

I'm New to to this and any help would be best for me. Please have a look and help a newbie out

You should filter the queryset itself, like:

class Idlist(LoginRequiredMixin,ListView):

    model=placement
    template_name='placement_list.html'

    def get_queryset(self):
        query_set=super().get_queryset()
        return query_set.

So we apply a .filter(..) on the query_set such that we only obtain placement s with user is self.request.user .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM