Struggling with pulling information out of a class in models.py (Thank you in advance for any help!)

19 views
Skip to first unread message

coreyj...@gmail.com

unread,
Apr 14, 2018, 11:55:21 PM4/14/18
to Django users
Dear all,

I am really struggling with pulling information out of a class object. I have tried doing this with FK field, proxy models (shown below as I feel this is the closest to working), etc. and cannot get it to work. I cannot pull the information from my database. I would greatly appreciate help. What I want is to have access to all of these fields independently. I feel like what I have should work but I cannot get the information out of my proxy models (shown below). I see what I want in the admin, however. How can I get the information out independently? 

class aichatbox_input2(models.Model):    
    aichatbox_input2_text = models.CharField(max_length=2000) 
    def __str__(self):
        return self.aichatbox_input2_text


class aichatbox_response2(models.Model):    
    image_url2 =models.CharField(max_length=1000, default='https://www.') 
    response2_text = models.CharField(max_length=1000) 
    aichatbox_response_input2 = models.ManyToManyField(aichatbox_input2)  
    views_aichatbox = models.IntegerField(default=0)
    pw_aichatbox = models.CharField(max_length=12, default='000000')
    link_aichatbox = models.CharField(max_length=250, default='None')
    nickname_aichatbox = models.CharField(max_length=100, default='Pseudonym')
    email_aichatbox = models.EmailField(max_length=100, default='bl...@blank.com')

    

    def __str__(self):
     
        return u'%s %s' % (self.response2_text, self.image_url2) 
  
class image_url2_class(aichatbox_response2):
    class Meta:
        proxy = True
    def __str__(self):
        return self.image_url2

class response2_text_class(aichatbox_response2):
    class Meta:
        proxy = True
    def __str__(self):
        return self.response2_text

class aichatbox_response_input2_class(aichatbox_response2):
    class Meta:
        proxy = True
    def __str__(self):
        return self.aichatbox_response_input2

class views_aichatbox_class(aichatbox_response2):     
    class Meta:
        proxy = True
    
    def __str__(self):
        return str(self.views_aichatbox)            # is it best to have this be str() here? I don't know of a better way to do this...

class pw_aichatbox_class(aichatbox_response2):
    class Meta:
        proxy = True
    def __int__(self):
        return self.pw_aichatbox

class link_aichatbox_class(aichatbox_response2):
    class Meta:
        proxy = True
    def __str__(self):
        return self.link_aichatbox

class nickname_aichatbox_class(aichatbox_response2):
    class Meta:
        proxy = True
    def __str__(self):
        return self.nickname_aichatbox

class email_aichatbox_class(aichatbox_response2):
    class Meta:
        proxy = True
    def __str__(self):
        return self.email_aichatbox

Derek

unread,
Apr 18, 2018, 11:20:15 AM4/18/18
to Django users

You will need to gve an example of your struggles (witth code being used to pull info, and with error traces) before we can help; all you have shown is model code.

coreyj...@gmail.com

unread,
Apr 18, 2018, 6:38:42 PM4/18/18
to Django users
Hi! Thanks for your response! I feel I have gone about this in a very odd way. I don't think that it is actually the answer but it works now. 
I am able to get the information out now without errors. What is your thought approaching it this way? Thank you for your time and help!
I am hoping that someone might be able to tell me a better way to do this:
 
Counter = aichatbox_response2.objects.filter().count()
for i in range(1,Counter+1):
        v = views_aichatbox_class
        n = nickname_aichatbox_class
        #i = image_url2_class
        r = response2_text_class
        a = aichatbox_input2_class
        p = pw_aichatbox_class
        l = link_aichatbox_class
        e = email_aichatbox_class
        if i == 1:
            v2 = str(v.objects.all()[:i])
            n2 = str(n.objects.all()[:i])
            #i2 = str(i.objects.all()[:i])
            #r2 = str(r.objects.all()[:i])
            a2 = str(a.objects.all()[:i])
            #p2 = str(p.objects.all()[:i])
            l2 = str(l.objects.all()[:i])
            e2 = str(e.objects.all()[:i])
        else:
            v2 = str(v.objects.all()[i-1:i])
            n2 = str(n.objects.all()[i-1:i])
            a2 = str(a.objects.all()[i-1:i])
            
            l2 = str(l.objects.all()[i-1:i])
            e2 = str(e.objects.all()[i-1:i])
    
        v2 = v2[35:len(v2)-3]
        n2 = n2[38:len(n2)-3]
        
        
        a2 = a2[36:len(a2)-3]
        
        l2 = l2[34:len(l2)-3]
        e2 = e2[35:len(e2)-3]

Best wishes,

Corey
Reply all
Reply to author
Forward
0 new messages