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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:04:45+00:00 2026-05-27T21:04:45+00:00

I have a custom django admin page, and I would like to make the

  • 0

I have a custom django admin page, and I would like to make the two ForeignKey fields optional in the admin interface. I do not want to change the underlying model.

This is the model:

class IncorporationTicket(models.Model, AdminURL):
    ordered_by = models.ForeignKey('Organisation', # organisation which ordered this
                                   null = True,
                                   blank = False, # i.e. can only be null as a result of delete
                                   on_delete = models.SET_NULL)
    ordered_by_individual = models.ForeignKey('Individual', # individual at organisation which ordered this
                                               null = True,
                                               blank = False, # i.e. can only be null as a result of delete
                                               on_delete = models.SET_NULL)

(AdminURL is a mixin which provides get_absolute_url)

This is the ModelAdmin:

class TicketAdmin(admin.ModelAdmin):
    readonly_fields = ('ordered', 'charge', 'amount_paid', 'submitted_on')

    formfield_overrides = {
        models.ForeignKey: {'required': False},
        }


    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        pk = resolve(request.path).args[0] # the request url should only have one arg, the pk
        instance = self.get_object(request, pk) 
        user = request.user
        kwargs['required'] = False # will be passed to every field

        if db_field.name == "ordered_by_individual":
            # queryset should be a union of (a) individual already set on object (b) individual for current user
            ## None option is provided by admin interface - just need to let field be optional.
            if instance.ordered_by_individual:
                kwargs["queryset"] = (
                    Individual.objects.filter(pk = instance.ordered_by_individual.pk) |
                    user.individual_set.all())
            else: kwargs["queryset"] = user.individual_set.all()
        elif db_field.name == "ordered_by":
            # queryset should be a union of (a) organisation already set (b) any organisations for which user is authorised

           try:
               individual = user.individual_set.all()[0]
               all_orgs = Organisation.all_organisations_for_which_individual_authorised_to_incorporate(individual)
           except:
               all_orgs = Organisation.objects.none()
           if instance.ordered_by:
               kwargs["queryset"] = (
                   Organisation.objects.filter(pk = instance.ordered_by.pk) |
                   all_orgs)
           else: kwargs["queryset"] = all_orgs

        return super(type(self), self).formfield_for_foreignkey(db_field, request, **kwargs)

As you can see, I have tried to use both formfield_overrides, and formfield_for_foreignkey to set required = False on the FormField, but it is not having the required effect: when attempting to save through the admin interface without setting (that is, leaving the field in its original blank state), the admin interface shows the error ‘This field is required.’

So, my question is: how does one prevent the underlying form from requiring certain fields, while still also setting the choices in formfield_for_foreignkey?

  • 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-27T21:04:46+00:00Added an answer on May 27, 2026 at 9:04 pm

    While I’m not sure why kwargs['required'] wouldn’t work, you could always override the admin form with your own form. It hasn’t failed me with magical django admin behavior so it’s a pretty good bet.

    class MyForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            super(MyForm, self).__init__(*args, **kwargs)
            self.fields['my_fk_field'].required = False
    
        class Meta:
             model = MyModel
    
    class MyAdmin(admin.ModelAdmin):
        form = MyForm
    

    This would still allow you to modify the QuerySet via the formfield_for_foo methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model where I would like to make a custom admin change_form
I have created a site mainly using django's admin interface, plus a few custom
I have a django admin interface and in the model listing I want a
I'm creating some custom views for the Django admin interface that use the standard
Ok so I have a custom django admin built from a Author Model: class
I have a reasonably complex custom Django model method. It's visible in the admin
I would like to have a custom snippet of html form code that takes
In my django admin.py I have a custom widget based on a select menu.
I have this line in a custom django-admin command: logger.info(%d records updated, records_updated) When
My problem is similar to Django Passing Custom Form Parameters to Formset Ive have

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.