简体   繁体   中英

From User can't query models.Model

When Using query from shell I got :

>>> 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'

From the admin interface, I have already assigned a choice. And I'm not able to see it with Query

model :

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

I'm currently not able to know which choice was assigned to the User model from the shell.

agence is an attribute of UserProfile not of User . Try with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM