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 7067977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:16:02+00:00 2026-05-28T05:16:02+00:00

Let’s assume that I have following models: class ScoutBook(models.Model): troop = models.ForeignKey(‘Dictionary’, limit_choices_to={‘type’ :

  • 0

Let’s assume that I have following models:

class ScoutBook(models.Model):
       troop = models.ForeignKey('Dictionary', limit_choices_to={'type' : 'Troop'},  related_name='+', blank=True, null=True)

class Dictionary(models.Model): 
     name = models.CharField(max_length=CHAR_FIELD_MAX_LEN, verbose_name="Nazwa")
     active = models.BooleanField(verbose_name="Aktywny")
     type = models.CharField(max_length=CHAR_FIELD_MAX_LEN, choices=DICTIONARY_CHOICES)

and I want to implement following logic:

when creating ScoutBook allow users to select only active troops, and when editing allow to select active troops or allow user to leave value unchanged (even if the troop is inactive). If I use limit_choices_to = {..., 'active' = True} troop that is inactive is absent from combo box in django admin.

So to be clear: let’s assume that there are four troops in this system: Troop1, Troop2 and InactiveTroop, InactiveTroop2. On model creation I would like user to be able to choose Troop1 and Troop2. If model has troop field set to InactiveTroop2, I would like user to be able to choose between InactiveTroop2, Troop1 and Troop2.

I was looking at the django forms api and I didn’t found obvious way do this. Moreover, in the application I’m developing there will be many such fields and many such models — so solution must be pain free. I would rather not create new Form class for every model. I will be using mostly django admin to enable editing the database, and some read only views that will just list entries.

Ideally I would like to encapsulate this functionality in some custom field — but fields have access to model instance on validate and save phase — so I dont have access to it when I produce formfield.

  • 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-05-28T05:16:03+00:00Added an answer on May 28, 2026 at 5:16 am

    Well I had to hook into ModelForm creation. Attached Form inspects it’s fields and if specific conditions are met it replaces model field queryset.

        class DictionayModelForm(ModelForm):
            def __init__(self, *largs, **kwargs):
                super(DictionayModelForm, self).__init__(*largs, **kwargs)
                if self.instance and self.instance.pk is not None:
                    for f in self.instance._meta.fields:
                        if isinstance(f, models.ForeignKey) and issubclass(f.rel.to, Dictionary):
                            model_field = self.fields[f.name]
                            value = getattr(self.instance, f.name, None)
                            if value and value not in model_field.choices:
                                model_field.queryset = Dictionary.objects.filter(Q(**f.rel.limit_choices_to) | Q(id = value.id))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have the following entity: public class Store { public List<Product> Products
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's say that I have classes like this: public class Parent { public int
Let me try to explain what I need. I have a server that is
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's assume that we are building a high traffic site that will be used

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.