Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use field value in limit_choices_to in Django

I have two models Project and Group. My groups belong to a specific project. My groups have the fields project = ForeignKey(Project) and parent = ForeignKey('self').

Can I use limit_choices_to to make sure the options in foreign key parent only consist of groups inside the same project?

I'm thinking of something like

def limit_choices_to(self):
    return {'project': self.project}
like image 701
Jamgreen Avatar asked Mar 06 '15 14:03

Jamgreen


2 Answers

This is impossible to do at the model level but you can change the queryset for this field in the form's constructor.

class GroupForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(GroupForm, self).__init__(*args, **kwargs)
        if self.instance.project:
            self.fields['parent'].queryset = Group.objects.filter(
                                                project=self.instance.project)

UPDATE: To do it in the admin you have to set the form attribute of the ModelAdmin:

class GroupAdmin(admin.ModelAdmin):
    form = GroupForm
like image 151
catavaran Avatar answered Sep 28 '22 06:09

catavaran


For the admin section I found hacky way to do it. My example is with admin.StackedInline, but almost sure it will work with plain ModelAdmin. I left it here just in case someone likes this solution:

class ProfessionalInline(admin.StackedInline):
model = Professional

fieldsets = [
    ('Research information', {
        'fields': ('team', 'projects')
    }),
    ('Profile information', {
        'fields': ('age', 'gender', 'receive_emails')
    })]

def get_parent_object_from_request(self, request):
    """
    Returns the parent object from the request or None.

    Note that this only works for Inlines, because the `parent_model`
    is not available in the regular admin.ModelAdmin as an attribute.
    """
    resolved = resolve(request.path_info)
    if resolved.kwargs:
        return self.parent_model.objects.get(pk=resolved.kwargs['object_id'])
    return None

def formfield_for_manytomany(self, db_field, request, **kwargs):
    '''Extremely hacky'''
    account = self.get_parent_object_from_request(request)
    if db_field.name == 'projects':
        kwargs['queryset'] = Project.objects.filter(team=account.professional.team)
    return super().formfield_for_manytomany(db_field, request, **kwargs)
like image 20
j4n7 Avatar answered Sep 28 '22 07:09

j4n7