Django 根据选定的 FK 限制多对多查询集 [英] Django Limit ManytoMany queryset based on selected FK

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

问题描述

我有一个看起来像这样的模型:

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)

因此,每当创建新事件时,都会随之创建一堆角色.在 Invite 模型中,如何仅显示与我在 Django Admin 的更改表单中选择的事件相关联的角色,而不是显示角色模型中的所有条目?

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?

推荐答案

你想动态过滤roles choices的选择,所以你需要ajax 来执行这个任务.

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发送event_id到你的自定义view 通过 ajax.

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

2:来自 Roles 模型 filter 基于您从 ajax 获得的 event_id 请求并通过 serializing 将过滤后的 roles 返回为 JSON.

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:清除现有的roles并通过解析JSON响应重新填充.

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

例如:这是一个 jquery getJSON 例子

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);

网址:

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

观看次数:

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

这只是一个使用 jquery 的示例,您可以使用任何类型的 ajax 来实现.

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

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

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