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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:38:38+00:00 2026-05-25T20:38:38+00:00

all. I’m working on the admin for my django site, and I’ve run into

  • 0

all. I’m working on the admin for my django site, and I’ve run into an obstacle.

I’ve got an Entry model and a Related model. The Related model has two foreign key fields: one to the Entry model (entry) and one to django’s User model (author). The Related model is considered a “sub-model” of the Entry model, and each user can only have one Related per Entry.

In the admin, Related is edited inline with Entry. As I have it, the admin shows only one extra Related at a time, and it automatically fills the author field with the current user:

from django.contrib import models
from django.contrib.auth.models import User

class Entry(models.Model):
    pass

class Related(models.Model):
    entry = models.ForeignKey(Entry)
    author = models.ForeignKey(User)
    class Meta:
        unique_together = ('entry', 'author')




from django.contrib import admin

class RelatedInline(admin.StackedInline):
    model = Related
    exclude = ('author',)
    max_num = 1

class EntryAdmin(admin.ModelAdmin):
    inlines = (RelatedInline,)
    def save_formset(self, request, form, formset, change):
        instances = formset.save(commit=False)
        for instance in filter(lambda obj: isinstance(obj, Related), instances):
             if instance.__dict__.get('author', None) is None:
                 instance.author = request.user
                 instance.save()
        formset.save_m2m()

The problem is that if a user wants to edit an entry which already has a Related by anyone, then only that one related field will be shown.

If possible, I wonder if anyone has any ideas about how I could keep a setup similar to this, but have the admin automatically display the user’s related if it exists and an empty form if it doesn’t. Barring that, I would just get rid of the line max_num = 1 and replace it with extra = 1. Of course, this would mean that a “new related” form would show even if the user already had one for the current entry, so I wonder if anyone has any idea about how I would catch a possible IntegrityError and let the user know that an error had occurred.

  • 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-25T20:38:38+00:00Added an answer on May 25, 2026 at 8:38 pm

    It turns out this is pretty simple. You just need to add a queryset function to your RelatedInline class, specifying which inline to show. If the returned queryset has at least one member, the first will be shown. If the queryset is empty, a single blank inline will be shown!

    class RelatedInline(admin.StackedInline):
        model = Related
        exclude = ('author',)
        max_num = 1
    
        def queryset(request):
            return Related.objects.filter(author = request.user)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I'm working on adding a glossary to an existing site. I'm planning to
All, I'm trying to split my MSTest test run into multiple runs because I'm
All, I currently have two projects that are under SourceSafe that I am unable
All of my users will be unapproved until they are approved by an admin,
All, I have some script tags that are not working in Wordpress. If I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
All, I am building a small site using PHP. In the site, I receive
All, I'm using wordpress with this and for some reason the eventDrop stopped working.
All, I'm using Facebook Connect (JS SDK) to authenticate a user to my site.
All, I have a guestbook feature on my site and I keep getting spammed.

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.