Wednesday, July 30, 2014

Re: Access to custom fields in view?

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

On Wednesday, July 30, 2014 at 11:02 AM, Miguel Angel Yáñez Camacho wrote:

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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/5ABC7736D24A460AB3DCD6207E103EE4%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment