Access to custom fields in view?

24 views
Skip to first unread message

Miguel Angel Yáñez Camacho

unread,
Jul 30, 2014, 2:02:00 PM7/30/14
to django...@googlegroups.com
Hi all from spain and sorry for my bad english ;)

Hi a have in this model 2 custom fields: "responsive" and "engine"

class template(models.Model):
    state
= models.IntegerField()  # estado del template 1= activo, 0= inactivo
    tid
= models.IntegerField(primary_key=True)
    price
= models.IntegerField()  # precio unidad
    priceexc
= models.IntegerField()  # precio exclusivo
    inserted_date
= models.DateTimeField(null=True, blank=True)
    update_date
= models.DateTimeField(null=True, blank=True)
    live_preview_url
= models.CharField(max_length=1000, null=True, blank=True)
    tipo
= models.ForeignKey(tipo)
    keywords
= models.CharField(max_length=9000, null=True, blank=True)
    screen_preview_s
= models.CharField(max_length=1000, null=True, blank=True)
    screen_preview_l
= models.CharField(max_length=1000, null=True, blank=True)
    origen
= models.ForeignKey(origen)
    url_compra
= models.CharField(max_length=1000, null=True, blank=True)
    promocionada
= models.BooleanField(default=False)
    portada
= models.BooleanField(default=False)

   
def __unicode__(self):
       
return self.live_preview_url

   
def _get_responsive(self):

       
try:
            responsive
= templates_propiedad.objects.get(propiedad="features", valor="responsive", plantilla=self)
            salida
= "1"
       
except:
            salida
= "0"
       
return salida
    responsive
= property(_get_responsive)

   
def _get_engine(self):
       
try:
            engine
= templates_propiedad.objects.get(propiedad="prestashop-compatibility", plantilla=self)
            salida
= engine.valor
       
except:
            salida
= "0"
       
return salida

    engine
= property(_get_engine)

in my view in this code i can access to a regular field but i CANT access to custom fields, give me error
            plantillas_db = templates_keyword.objects.select_related('plantilla').filter(keyword=bd_keywordsfinal, plantilla__state='1', plantilla__promocionada=False).distinct().order_by('-plantilla__inserted_date')

            s
= set(plantillas_db)
           
for i in s:
               
print(i.id)
               
print(i.responsive)
               
print(i.engine)

How can i access to this fields?
thanks

Miguel

Ryan Blunden

unread,
Jul 30, 2014, 2:56:36 PM7/30/14
to django...@googlegroups.com
I would use the @property decorator.

class template(models.Model):
    ...    
    
    @property
    def responsive(self):


        
try:
            responsive 
= templates_propiedad.objects.get(propiedad="features", valor="responsive", plantilla=self)
            salida 
= "1"
        
except:
            salida 
= "0"
        
return salida


    
@property
    def engine(self):

        
try:
            engine 
= templates_propiedad.objects.get(propiedad="prestashop-compatibility", plantilla=self)
            salida 
= engine.valor
        
except:
            salida 
= "0"
        
return salida
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6177a2f8-f2d7-4b88-bc83-fc2d9bccf284%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Roseman

unread,
Jul 31, 2014, 4:19:51 AM7/31/14
to django...@googlegroups.com
On Wednesday, 30 July 2014 19:02:00 UTC+1, Miguel Angel Yáñez Camacho wrote:

in my view in this code i can access to a regular field but i CANT access to custom fields, give me error
            plantillas_db = templates_keyword.objects.select_related('plantilla').filter(keyword=bd_keywordsfinal, plantilla__state='1', plantilla__promocionada=False).distinct().order_by('-plantilla__inserted_date')

            s
= set(plantillas_db)
           
for i in s:
               
print(i.id)
               
print(i.responsive)
               
print(i.engine)

How can i access to this fields?
thanks

Miguel

*What* error you you get *exactly*? The code you've given should be fine, although it's cleaner to use the @property decorator rather than manually wrapping a function (but the functionality is exactly the same). Show us what happens.
--
DR.
Reply all
Reply to author
Forward
0 new messages