如何对多个到多个字段的get_absolute_url进行反向操作?

时间:2022-09-04 12:33:50

I have a model with many-to-many fields and I need to get an ID selected from a many to many field. I decorated a get_absolute_url method with permaling decorator. And it doesn't work. So I understand that I need to reverse the relation, it is obvious from the trace, but I do not really understand what should I do?

我有一个包含多对多字段的模型,我需要从多个字段中选择一个ID。我用permaling装饰器修饰了一个get_absolute_url方法。它不起作用。所以我理解我需要扭转这种关系,从痕迹中可以看出这一点,但我真的不明白我该怎么办?

Model:

class MenuItems(models.Model):
    reference_value = models.CharField(max_length=255)
    filter_ids = models.ManyToManyField(Filter, blank = True)

    def __unicode__(self):
        return u'%s' % self.reference_value

    @models.permalink
    def get_absolute_url(self): 
        return ('homepage_ids', None, {'ids': self.filter_ids })

I tried to do with the reverse(), but I have the behavior of the method didn't changed.

我尝试用reverse()做,但我的方法的行为没有改变。

    @models.permalink
    def get_absolute_url(self): 
        return reverse('homepage_ids', kwargs={'ids': self.filter_ids })

3 个解决方案

#1


1  

without seeing the url pattern.

没有看到网址模式。

self.filter_ids does not return a list of ids, something like.

self.filter_ids不返回id列表,例如。

self.filter_ids.all().values_list('id', flat=True)

would return [1,2,3]

会回来[1,2,3]

#2


0  

You didn't post your url but something like this, should work

你没有发布你的网址,但这样的事情应该有效

urls

url(r'^/something/(?P<var>\d+)/$', view_name, name="homepage_ids"),

models

@permalink
def get_absolute_url(self):
    return ('homepage_ids', [str(self.filter_ids)])

template

<a href="{{ ids.get_absolute_url }}"> {{ ids }}</a>

take a look into django tutorial

看看django教程

#3


0  

I did it this way: url

我是这样做的:网址

 url(r'^(?P<ids>\d(&\d)*)?/?$', 'homepage', name='homepage'),

models

class MenuItems(models.Model):
"""Menu items... What???"""
reference_value = models.CharField(max_length=255)
filter_ids = models.ManyToManyField(Filter, blank = True, related_name="filter_ids")

def __unicode__(self):
    return u'%s' % self.reference_value

def get_absolute_url(self):
    int_ids = list(self.filter_ids.all().values_list('id', flat=True))
    str_ids = "&".join([str(val) for val in int_ids])
    return reverse('homepage', kwargs = {'ids': str_ids, })

I killed @ Permalink, because the API Permalink decorator was unuseble, convert my IDs to string with a couple of steps and apply the reverse function. The problem of the same urls of different pages are still there, but it is not important for my application, because it never will be.

我杀了@ Permalink,因为API永久链接装饰器不可用,将我的ID转换为字符串,然后应用反向函数。不同页面的相同网址的问题仍然存在,但它对我的应用程序并不重要,因为它永远不会。

#1


1  

without seeing the url pattern.

没有看到网址模式。

self.filter_ids does not return a list of ids, something like.

self.filter_ids不返回id列表,例如。

self.filter_ids.all().values_list('id', flat=True)

would return [1,2,3]

会回来[1,2,3]

#2


0  

You didn't post your url but something like this, should work

你没有发布你的网址,但这样的事情应该有效

urls

url(r'^/something/(?P<var>\d+)/$', view_name, name="homepage_ids"),

models

@permalink
def get_absolute_url(self):
    return ('homepage_ids', [str(self.filter_ids)])

template

<a href="{{ ids.get_absolute_url }}"> {{ ids }}</a>

take a look into django tutorial

看看django教程

#3


0  

I did it this way: url

我是这样做的:网址

 url(r'^(?P<ids>\d(&\d)*)?/?$', 'homepage', name='homepage'),

models

class MenuItems(models.Model):
"""Menu items... What???"""
reference_value = models.CharField(max_length=255)
filter_ids = models.ManyToManyField(Filter, blank = True, related_name="filter_ids")

def __unicode__(self):
    return u'%s' % self.reference_value

def get_absolute_url(self):
    int_ids = list(self.filter_ids.all().values_list('id', flat=True))
    str_ids = "&".join([str(val) for val in int_ids])
    return reverse('homepage', kwargs = {'ids': str_ids, })

I killed @ Permalink, because the API Permalink decorator was unuseble, convert my IDs to string with a couple of steps and apply the reverse function. The problem of the same urls of different pages are still there, but it is not important for my application, because it never will be.

我杀了@ Permalink,因为API永久链接装饰器不可用,将我的ID转换为字符串,然后应用反向函数。不同页面的相同网址的问题仍然存在,但它对我的应用程序并不重要,因为它永远不会。