django - Class meta got invalid attributes -



django - Class meta got invalid attributes -

please help prepare meta class value gave after detailed research. getting error while trying handle template urls "get_absolute_url" responds next error.

typeerror: 'class meta' got invalid attribute(s): sale_price,get_absolute_url.

below code.

class meta: db_table = 'products' ordering = ['-created_at'] def __unicode__(self): homecoming self.name @models.permalink def get_absolute_url(self): homecoming ('catalog_product', (), {'product_slug': self.slug}) def sale_price(self): if self.old_price > self.price: homecoming self.price else: homecoming none

thanks.

you misunderstanding how models defined. add together methods , attributes actual model class , utilize meta class specify options upon class:

class mymodel(models.model): old_price = ... cost = ... slug = ... created_at = ... ... def __unicode__(self): homecoming self.name @models.permalink def get_absolute_url(self): homecoming ('catalog_product', (), {'product_slug': self.slug}) def sale_price(self): if self.old_price > self.price: homecoming self.price else: homecoming none class meta: db_table = 'products' ordering = ['-created_at']

have read of model documentation , pay attending section on meta options

edit

also, don't utilize permalink decorator it's no longer recommended:

https://docs.djangoproject.com/en/1.6/ref/models/instances/#the-permalink-decorator

the permalink decorator no longer recommended. should utilize reverse() in body of get_absolute_url method instead.

django python-2.7

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -