jeudi 1 août 2019

Django Object Names , can't compare values

the problem about 2 things ; First of all in admin panel and webpage foreign keys are shown as object(1) ,Yes I know the solution of this we use str stuff but when I try to compare id of them it takes the value of str function result let me show in code; Im checking logged in user's connected company id that equals to only students who are connected with same company

model.py - Student

class Student(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='StudentID')
    parentid = models.ForeignKey(Parent, on_delete=models.CASCADE, verbose_name='ParentID')
    companyid = models.ForeignKey(Company, on_delete=models.CASCADE, verbose_name='CompanyID')
    classid = models.ForeignKey(Classes, on_delete=models.CASCADE, verbose_name='ClassID')
    gender = models.CharField(max_length=1, default='N', verbose_name='Gender')
    name = models.CharField(max_length=30, verbose_name='Name')
    surname = models.CharField(max_length=30, verbose_name='Surname')
    dob = models.DateTimeField(verbose_name='Doğum Yılı')
    bloodtype = models.ForeignKey(BloodType, on_delete=models.CASCADE, verbose_name='Blood Type')
    status = models.BooleanField(verbose_name='State')

    list_display = ('id', 'parentid', 'companyid', 'classid', 'gender', 'name', 'surname', 'dob', 'bloodtype', 'status')

    def __str__(self):
        return "%s  %s - %s" % (self.name, self.surname, self.gender)

model.py - Company

class Company(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='CompanyID')
    name = models.CharField(
        max_length=100, verbose_name='Company Name')
    contactname = models.CharField(max_length=30)
    contactsurname = models.CharField(
        max_length=30)
    address = models.CharField(max_length=200)
    city = models.CharField(max_length=20)
    phone = models.CharField(max_length=12)
    weburl = models.URLField(max_length=80)
    email = models.CharField(max_length=80)
    studentcapacity = models.BigIntegerField(verbose_name='Student Capacity')
    classcapacity = models.BigIntegerField(verbose_name='Class Capacity')
    notes = models.TextField(max_length=200)
    status = models.BooleanField(verbose_name='State')

    list_display = ('id', 'name', 'contactname', 'contactsurname', 'address', 'city', 'phone',
                    'weburl', 'email', 'studentcapacity', 'classcapacity', 'notes', 'status')

    def __str__(self):
        return "%s -  %s" % (self.id, self.name)


views.py

@login_required()
def student_update(request, pk):
    student = get_object_or_404(Student, pk=pk)

    usercompanyid = UserProfile.objects.filter(userid=request.user.id).only('companyid')
    print('before control')

    a = usercompanyid.values_list('companyid', flat=True)[0]
    print(a)

    print(student.companyid)

    if student.companyid == a:

        print('passed if')
        if request.method == 'POST':
            form = StudentForm(request.POST, instance=student)
        else:
            form = StudentForm(instance=student)
            return save_student_form(request, form, 'student_update_partial.html')
    else:
        return view_404(request)

at if state " student.companyid == a: " = Company ID - Company Name , output: 1 -Test CompanyName = 1 , I need it to return only field I call which is companyid, I know it is allias to "companyID - companyName". Is it possible to fix this with another way , I'm not pro of django python , just trying to do some project for learning if it's a dump question sorry for all :)




Aucun commentaire:

Enregistrer un commentaire