2012-03-07 5 views
0

したがって、ManyToManyオブジェクトを絶対URLに追加する際に問題が発生しています。私のモデルは以下の通りです。 Categoryクラスをチェックしてください。 URLにカテゴリタイトルを付ける必要があることはわかっています。Django:ManyToManyオブジェクトを持つ絶対URL

# Category class 
class Category(models.Model): 
    title = models.CharField(max_length=128) 
    description = models.TextField() 

    def __unicode__(self): 
    return self.title 


# Product class 
class Product(models.Model): 
    title = models.CharField(max_length=128) 
    pub_date = models.DateField('date published') 
    slug_title = models.SlugField(editable=False) 
    cover_image = models.ImageField(upload_to="images/product/") 
    image = models.ManyToManyField(Image) 
    category = models.ManyToManyField(Category) 
    description = models.TextField() 

    def save (self):    
    self.slug_title = slugify(self.title) 
    super(Product, self).save() 

    def __unicode__(self): 
    return self.title 

def get_absolute_url(self): 
    return ('product_view_url',(), { 
     'category': self.category, 
     'year': self.pub_date.strftime("%Y"), 
     'month': self.pub_date.strftime("%m"), 
     'day': self.pub_date.strftime("%d"), 
     'id': self.id, 
     'slug_title': self.slug_title }) 

get_absolute_url = models.permalink(get_absolute_url) 

ここproduct_view_urlためurl.pyです:

以下
url(r'^product/(?P<category>.+)/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<id>.+)/(?P<slug_title>.+)/$', index_views.product_detail, name='product_view_url'), 

は図である。

# Product Detail View 
    def product_detail(request, id, category, slug_title, year, month, day, template='index/product_detail.html'): 

    product = get_object_or_404(Product, id=id, slug_title=slug_title) 

    payload = {'product': product} 

    return render_to_response(template, payload, context_instance=RequestContext(request)) 

この構成では、私は奇妙なURLを取得する:「のhttp:// localhostと: 8000 /製品/%3Cdjango.db.models.fields.related.ManyRelatedManager%20オブジェクト%20at%200x1067d6050%3E/2012/03/07/1/tropical-twizzler/'

「http:// localhost:8000/product/candies/2012/03/07/1/tropical-twizzler /」

私はまだnoobであり、まだ自分のやり方を学んでいますDjangoの周り。

+0

あなたはあなたのurls.pyを投稿できますか?私はあなたが '' product_view_url ''をどのように定義しているか見る必要があります。 – tamakisquare

+0

@ahmoo投稿を更新しました。 – Infinixd

答えて

1

問題はcategoryはManyToManyFieldです。定義上、ManyToManyFieldに直接アクセスすると、単一の値ではなく値のセットが得られます。ManyRelatedManagerマネージャのインスタンスから値を取り出すには、hereを把握する必要があります。