Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9233357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:32:06+00:00 2026-06-18T06:32:06+00:00

MyModel has a mptt’s TreeForeignKey field category . In the admin of MyModel ,

  • 0

MyModel has a mptt’s TreeForeignKey field category. In the admin of MyModel, how can I disable some options in the Select widget of this category field?

Thanks you

class Category(MPTTModel):
    parent = TreeForeignKey('self', blank=True, null=True, related_name='children')
    nom = models.CharField(max_length=100)

class MyModel(models.Model):
    category = TreeForeignKey(Category)
    # more fields
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:32:07+00:00Added an answer on June 18, 2026 at 6:32 am

    I had to overwrite MyModelAdminForm and use a forms.ChoiceField field with a SelectWithDisabled widget for the category field. In my case I only wanted the leaves of the tree to be selectable.

    class MyModelAdminForm(forms.ModelForm):
    
        class Meta:
            model = MyModel
    
        def __init__(self, *args, **kwargs):
            self.level_indicator = kwargs.pop('level_indicator', u'---')
            super(MyModelAdminForm, self).__init__(*args, **kwargs)
    
            queryset = Category.objects.all()
            mptt_opts = queryset.model._mptt_meta
            queryset = queryset.order_by(mptt_opts.tree_id_attr, mptt_opts.left_attr)
    
            choices = []
            for item in queryset:
                level = getattr(item, item._mptt_meta.level_attr)
                value = item.id
                label = mark_safe(conditional_escape(self.level_indicator) * level + smart_unicode(item.nom))
                if item.is_leaf_node():
                    choices.append((value, label))
                else:
                    choices.append((value, {'label': label, 'disabled': True}))
    
            self.fields['category'] = forms.ChoiceField(choices=choices, widget=SelectWithDisabled)
    
    
    class MyModelAdmin(admin.ModelAdmin):
        form = MyModelAdminForm
    
    samsic_site.register(MyModel, MyModelAdmin)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a model MyModel with a category field. The Category model has a
I can fetch the data like this. value= mymodel.objects.get(anycondition) OR value= mymodel.objects.filter(anycondition) and can
I have a django application which has two models like this: class MyModel(models.Model): name
The situation is something like this: PostgreSQL Database table has a field dateAdded which
I have a model MyModel that has a field expiration_datetime. Every time a user
Given this class: class MyModel < ActiveRecord::Base belongs_to :association1 belongs_to :association2, :polymorphic => true
@model MyMVC.Models.MyMVC.MyModel @{ ViewBag.Title = Index; } The reason why I ask this question
I know this has been asked before, and I have it working, but I'm
I need to get the duplicate entries of field title in model Mymodel where
My model and collection looks like this. var MyModel= Backbone.Model.Extend({ foo: function(){ alert(is not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.