基于选定的FK,Django限制多个多个查询器 [英] Django Limit ManytoMany queryset based on selected FK

查看:125
本文介绍了基于选定的FK,Django限制多个多个查询器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的模型:

  class Invite(models.Model):
user = models.ForeignKey(User)
event = models.ForeignKey(Event)
roles = models.ManyToManyField(Role,blank = True,null = True)
sent = models.BooleanField(Invite发送,默认= False,可编辑= False)
created = models.DateTimeField(auto_now_add = True)

def __unicode __(self):
return u%s% self.user

class Meta:
unique_together =(('user','event'),)


类角色(models.Model )

这将用户角色与事件相关联

event = models.ForeignKey(Event,related_name =roles)
roletype = models.ForeignKey(RoleType)
profiles = models.ManyToManyField(Profile,related_name =roles,
blank = True,null = True)
modified = models.DateTimeField(auto_now = True)
created = models.DateTimeField(auto_no w_add = True)

所以每当创建一个新的事件时,都会创建一堆角色。在邀请模型中,如何只显示与Django Admin中更改表单中选择的事件相关的角色,而不是显示Role模型中的所有条目?

解决方案

您想要动态过滤角色的选择 选择 ,所以您将需要 ajax 来执行此任务。



以下是如何使此工作。 通过 ajax event_id 发送到您的自定义视图



2:角色模型根据 ajax 请求获得的 event_id ,然后返回过滤的 roles by serializing into JSON



3:清除现有的角色,并通过 JSON 回复。



例如:
这是一个 jquery getJSON 示例



javascript:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ (/ my-app / my-roles-filter-view /+ event_id +/,function(data){
var roles_dd = $(#roles);
$(' #event>选项')。remove();
$ .each(data,function(index,value){
roles_dd.append($(< option />\").val (value).text(value));
});
})(django.jquery);

url's:

 ('^ / my-app / my -roles-filter-view /(?P< event_id> \d +)/ $','my_view'),

视图

  qs = Role.objects.filter(event = event_id).values_list('id')
return HttpResponse(simplejson.dumps(qs),mimetype ='application / javascript')

这只是一个使用 jquery 您可以使用任何类型的 ajax 并实现此目的。


I have a model that looks like this:

class Invite(models.Model):
    user = models.ForeignKey(User)
    event = models.ForeignKey(Event)
    roles = models.ManyToManyField(Role, blank=True, null=True)
    sent =  models.BooleanField("Invite Sent", default=False, editable=False)
    created = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return u"%s" % self.user

    class Meta:
        unique_together =(('user','event'),)


class Role(models.Model):
    """
    This associates a user's role to an event
    """
    event = models.ForeignKey(Event, related_name="roles")
    roletype = models.ForeignKey(RoleType)
    profiles = models.ManyToManyField(Profile, related_name="roles",
            blank=True, null=True)
    modified = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)

So whenever a new Event is created, a bunch of roles gets created along with it. In the Invite model, how can I only show the roles associated with the event I've selected in the change form within Django Admin instead of showing all the entries in the Role model?

解决方案

You want to dynamically filter the choices of the roles choices, so you will need ajax to perform this task.

Here's how you can make this work..

1: OnChange of the event send the event_id to your custom view through ajax.

2: From the Roles model filter based on the event_id you got from the ajax request and return the filtered roles by serializing into JSON.

3: Clear the existing roles and repopulate by parsing through the JSON response.

Eg: This a jquery getJSON example

javascript:

$("#event").change(function (){         
 var event_id = $(this).val();              
    $.getJSON("/my-app/my-roles-filter-view/"+ event_id +"/",function(data){
        var roles_dd = $("#roles");
        $('#event >option').remove();
        $.each(data, function(index,value) {
        roles_dd.append($("<option />").val(value).text(value));
    });                 
})(django.jquery);

url's:

('^/my-app/my-roles-filter-view/(?P<event_id>\d+)/$','my_view'),

views:

def my_view(request,event_id):
    qs = Role.objects.filter(event=event_id).values_list('id')
    return HttpResponse(simplejson.dumps(qs),mimetype='application/javascript')

This just an example using jquery you can use any type of ajax and achieve this.

这篇关于基于选定的FK,Django限制多个多个查询器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆