关于两件事的问题;首先,在管理面板和网页中,外键显示为object(1),是的,我知道解决方案我们使用 str 东西,但是当我尝试比较它们的ID时,它的取值为 str 函数结果让我在代码中显示; 我正在检查已登录用户的关联公司ID,该ID仅等于与同一公司关联的学生
model.py-学生
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-公司
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)
如果状态为“ student.companyid == a:” =公司ID-公司名称,输出:1 -Test CompanyName = 1,我需要它仅返回我称为companyid的字段,我知道它是“ companyID-companyName”的别名。是否可以用另一种方式解决此问题,我不是django python的专业人士,只是尝试做一些项目来学习这是否是一个对所有人都不好意思的转储问题:)
答案 0 :(得分:1)
这里only
和values
周围有很多不必要的混乱。不需要任何。只需直接比较对象即可:
def student_update(request, pk):
student = get_object_or_404(Student, pk=pk)
if student.companyid == request.userprofile.companyid:
...
顺便说一句,您不应将外键称为“ companyid”,“ parentid”等;它们不是ID,而是对象。称他们为“公司”,“父母”等。
答案 1 :(得分:1)
执行此操作时:
companyid = models.ForeignKey(...)
数据库架构实际上将具有companyid_id
字段名称。
因此,在您的情况下,如果要比较对象ID,则实际上需要这样做:
if student.companyid_id == a:
为避免混淆,我建议您不要将“ id”部分附加到Django模型字段名称中。