从用户无法查询模型。

时间:2019-05-21 14:42:17

标签: python django django-models django-users

使用shell查询时,我得到了:

>>> q = User.objects.get(username='TestUser')
>>> q.agence

Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'User' object has no attribute 'agence'

从管理界面,我已经分配了一个选项。而且我无法通过查询看到它

型号:

def CreateListDomaines():
    nameQuery = Domaine.objects.all().values_list('nomDomaine', flat=True)
    domaines = []

    i = 0
    for q in nameQuery :
        l = (str(i),q)
        domaines.append(l)
        i+=1

    return domaines

def CreateListAgences():
    nameQuery = Agence.objects.all().values_list('nomAgence', flat=True)
    agences = []

    i = 0
    for q in nameQuery :
        l = (str(i),q)
        agences.append(l)
        i+=1

    return agences

class Domaine(models.Model):
    nomDomaine = models.CharField(max_length=200,default=True)

    def __str__(self):
        return self.nomDomaine

class Agence(models.Model):
    nomAgence = models.CharField(max_length=200,default=True,null=False,blank=False)
    Ville = models.CharField(max_length=200, default=True,null=True,blank=True)
    Region = models.CharField(max_length=200,default=True,null=True,blank=True)
    Departement = models.CharField(max_length=200,default=True,null=True,blank=True)
    AgencetelephonePortable = PhoneNumberField(null=True,blank=True)

    def __str__(self):
        return self.nomAgence

class UserProfile(models.Model):
    domainesList = CreateListDomaines()
    agencesList = CreateListAgences()
    user = models.OneToOneField(User, on_delete=models.CASCADE,unique=True)
    domaine = models.CharField(choices=domainesList,max_length=50, null=True, blank=True)
    agence = models.CharField(choices=agencesList,max_length=50, null=True, blank=True)

    def __str__(self):
        return self.user.username

我目前无法从shell知道哪个选项已分配给User模型。

1 个答案:

答案 0 :(得分:1)

agenceUserProfile而不是User的属性。尝试:

>>> q = User.objects.get(username='TestUser')
>>> q.userprofile.agence