在forms.py中,我向用户显示表单。 Departamentos下拉列表显示了所有的部分,而不是那些属于特定empresa示例的部分。
我的意思是,通过使用此查询,它显示了表格中的所有条目,而不是它所属的位置。怎么会?如果我发送2个ID值以使查询具体化
id =当前用户ID
empresa =用户所属的当前公司ID
empleadoObject = empleado.objects.get(
id=request.session['member_id'],
empresa=request.session['company_id'])
form = UpdateEmpleadoForm(
request.POST, instance=empleadoObject)
form = UpdateEmpleadoForm(instance=empleadoObject)
models.py
class empresa(models.Model):
codigo = models.CharField(max_length=11)
nombre = models.CharField(max_length=50)
logo = StdImageField(upload_to='logo/%Y/%m/%d', variations={
'large': (300, 300),
'thumbnail': (280, 55, True)})
correo = models.EmailField(max_length=100)
telefono = models.CharField(max_length=21)
direccion = models.CharField(max_length=200)
usuario = models.CharField(max_length=15)
password = models.CharField(max_length=40)
def logoEmpresa(self):
return '<img src="/media/%s" height="90" width="90">' % (self.logo.thumbnail)
logoEmpresa.allow_tags = True
def __unicode__(self):
return self.nombre
class departamento(models.Model):
empresa = models.ForeignKey(empresa)
nombre = models.CharField(max_length=80)
def __unicode__(self):
return self.nombre
class empleado(models.Model):
empresa = models.ForeignKey(empresa)
nombre = models.CharField(max_length=50)
fecha_nacimiento = models.DateField(auto_now_add=False)
GENDER_CHOICES = (
('M', 'Masculino'),
('F', 'Femenino'),
)
sexo = models.CharField(max_length=1, choices=GENDER_CHOICES)
avatar = StdImageField(upload_to='avatar/%Y/%m/%d', variations={
'large': (300, 300, True),
'medium': (50, 50, True),
'thumbnail': (98, 122, True)})
correo = models.EmailField(max_length=100)
departamento = models.ForeignKey(departamento)
telefono = models.CharField(max_length=21)
direccion = models.CharField(max_length=200)
twitter = models.CharField(max_length=15)
usuario = models.CharField(max_length=15)
password = models.CharField(max_length=40)
primer_lugar = models.CharField(max_length=20)
segundo_lugar = models.CharField(max_length=20)
tercer_lugar = models.CharField(max_length=20)
goleador = models.CharField(max_length=20)
def avatarEmpleado(self):
return '<img src="/media/%s" height="90" width="90">' % (self.avatar.thumbnail)
avatarEmpleado.allow_tags = True
def __unicode__(self):
return self.nombre
答案 0 :(得分:0)
我不明白你的意思。如果要使用外键的属性进行查询,则应使用__attribute
empleadoObject = empleado.objects.get(
id=request.session['member_id'],
empresa__id=request.session['company_id'])
使用empresa__id即使它们不是外键也可以访问对象属性。试试吧。