Home > Net >  django how to get user to display specific object
django how to get user to display specific object

Time:07-28

I'm learning django and I'm trying to display all items from a specific user which is the 2003-111

itemid      sy      sem  resolve_by
book    2021-2022    2    2003-111
table   2021-2022    1    2012-455

here's my models.py I'm using customuser to use email as authentication instead of username and replace my id with userid(character)

class ClearanceCustomuser(models.Model):
password = models.CharField(max_length=128)
last_login = models.DateTimeField(blank=True, null=True)
is_superuser = models.BooleanField()
userid = models.CharField(primary_key=True, max_length=9)
email = models.CharField(unique=True, max_length=254)
is_staff = models.BooleanField()
is_active = models.BooleanField()
date_joined = models.DateTimeField()

class Meta:
    managed = False
    db_table = 'clearance_customuser'

class ClearanceItem(models.Model):
itemid = models.CharField(primary_key=True, max_length=20)
sem = models.CharField(max_length=1, blank=True, null=True)
sy = models.CharField(max_length=9, blank=True, null=True)
resolve_by = models.ForeignKey('ClearanceCustomuser', models.DO_NOTHING, 
db_column='resolve_by', blank=True, null=True)

class Meta:
    managed = False
    db_table = 'clearance_item'

for now this is my views.py

def index(request):
context = {}
context['items'] = ClearanceItem.objects.all()
return render(request, 'clearance/index.html', context)

which is displaying all items. I was thinking of something like this

select b.cl_itemid,b.sem,b.sy,b.resolve_by
from curriculum.clearance_customuser a
INNER JOIN curriculum.clearance_item b ON a.userid = b.resolve_by
where resolve_by = '2003-221'

CodePudding user response:

if 2003-111 is userid field of ClearanceCustomuser than you go like:

def index(request):
context = {}
context['items'] = ClearanceItem.objects.filter(resolve_by__userid='2003-111')
return render(request, 'clearance/index.html', context)

if you want to pass userid as parameter in yours urls.py edit path to index:

path('<str:userid>/', views.index, name='index')

and in view:

 def index(request, userid):
    context = {}
    context['items'] = ClearanceItem.objects.filter(resolve_by__userid=userid)
    return render(request, 'clearance/index.html', context)
  • Related