2012-03-07 3 views
0

그래서 절대 URL에 ManyToMany 객체를 추가하는 데 문제가 있습니다. 아래는 내 모델입니다. Category 클래스를 확인하십시오. 카테고리 제목을 URL로 가져와야한다는 것을 알고 있습니다.Django : ManyToMany 객체가있는 절대 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 : 아래

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'), 

뷰입니다 : 여기
# 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입니다 // 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 /'

죄송합니다. 아직 내 길을 배우고 있습니다. 장고 주위에.

+0

urls.py를 게시 할 수 있습니까? 나는 당신이 그것 안에''product_view_url ''을 어떻게 정의 하는지를 볼 필요가있다. – tamakisquare

+0

@ahmoo 게시물을 업데이트했습니다. – Infinixd

답변

1

문제는 category은 ManyToManyField입니다. 정의에 따라 ManyToManyField에 직접 액세스하는 것은 단일 값이 아니라 값 집합을 제공합니다. ManyRelatedManager. 관리자 인스턴스에서 값을 선택하려면 here을 알아낼 수 있어야합니다.